{"id":127,"date":"2014-09-23T00:07:53","date_gmt":"2014-09-22T15:07:53","guid":{"rendered":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2014\/09\/23\/active-record"},"modified":"2014-09-23T00:07:53","modified_gmt":"2014-09-22T15:07:53","slug":"active-record","status":"publish","type":"post","link":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2014\/09\/23\/active-record\/","title":{"rendered":"ActiveRecord\u3068MySQL\u3067\u5c11\u3057\u8907\u96d1\u306a\u691c\u7d22\u306eRailsAPI\u3092\u4f5c\u308b"},"content":{"rendered":"

\u6700\u8fd1\u793e\u5185\u306e\u798f\u5229\u539a\u751f\u3067\u3001\u300c\u5e74\u959310\u4e07\u5186\u3092\u4e0a\u9650\u3068\u3057\u3001\u696d\u52d9\u4e0a\u5fc5\u8981\u3001\u958b\u767a\u74b0\u5883\u306e\u52b9\u7387\u5316\u3084\u65b0\u3057\u3044\u77e5\u8b58\u7fd2\u5f97\u306b\u5f79\u7acb\u3064\u3068\u8003\u3048\u3089\u308c\u308b\u96fb\u5b50\u6a5f\u5668\u3084\u30b0\u30c3\u30ba\u306e\u8cfc\u5165\u304c\u53ef\u80fd\u300d\u3068\u3044\u3046\u3010\u696d\u52d9\u52b9\u7387\u5316\u5468\u8fba\u30b0\u30c3\u30ba\u8cfc\u5165\u88dc\u52a9\u3011\u306a\u308b\u3082\u306e\u304c\u767a\u8db3\u3057\u3001\u3055\u3063\u305d\u304f\u591c\u306a\u591c\u306aAmazon\u3067\u7f8e\u5473\u3057\u305d\u3046\u306aIT\u30ac\u30b8\u30a7\u30c3\u30c8\u304c\u306a\u3044\u304b\u63a2\u7d22\u3057\u3066\u3044\u308b\u524d\u7530\u3067\u3059\u3002<\/p>\n

\u5f0a\u793e\u3067\u306f\u73fe\u5728\u30d5\u30ed\u30f3\u30c8\u30b5\u30a4\u30c9\u306fAngularJS\u3001\u30b5\u30fc\u30d0\u30fc\u30b5\u30a4\u30c9\u306frails\u3067API\u3068CMS\u3001\u3068\u3044\u3046\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u958b\u767a\u3057\u3066\u304a\u308a\u307e\u3059\u3002
\u79c1\u306frails\u306e\u30b5\u30fc\u30d0\u30fc\u30b5\u30a4\u30c9\u306eAPI\u62c5\u5f53\u3068\u3044\u3046\u3053\u3068\u3067\u3001\u958b\u767a\u306e\u969b\u306b\u8a70\u307e\u3063\u305f\u3068\u3053\u308d\u306a\u3069\u3092\u5099\u5fd8\u9332\u304c\u3066\u3089\u66f8\u3044\u3066\u3044\u304d\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002
\nrails\u6b74\u306e\u77ed\u3044\u79c1\u3081\u306e\u5099\u5fd8\u9332\u304c\u3001\u5c11\u3057\u3067\u3082\u8aad\u3093\u3067\u304f\u3060\u3055\u3063\u305f\u65b9\u306e\u304a\u5f79\u306b\u7acb\u3066\u308c\u3070\u5e78\u3044\u3067\u3059\u3002<\/p>\n

\u305d\u308c\u3067\u306f\u65e9\u901f\u30bf\u30a4\u30c8\u30eb\u306b\u3042\u308b\u901a\u308a\u3001\u4e2d\u898f\u6a21\u7a0b\u5ea6\u306e\u30c7\u30fc\u30bf\u91cf\u304c\u5165\u3063\u305f\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u304b\u3089\u3001\u8272\u3005\u306a\u691c\u7d22\u306b\u5bfe\u5fdc\u3059\u308b\u30ed\u30b8\u30c3\u30af\u3092\u66f8\u3044\u3066\u3044\u304d\u307e\u3059\u3002
\n\u4f8b\u3068\u3057\u3066\u3001\u5168\u56fd\u30c1\u30a7\u30fc\u30f3\u306b\u306a\u3063\u3066\u3044\u308b\u672c\u5c4b\u3055\u3093\u306e\u672c\u3092\u3001\u4e0b\u8a18\u306e\u8981\u4ef6\u3092\u6e80\u305f\u3059\u3088\u3046\u306b\u5b9f\u88c5\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n

####\u2460\u672c\u306e\u30bf\u30a4\u30c8\u30eb\u3001\u8457\u8005\u540d\u3001\u51fa\u7248\u793e\u3001\u5024\u6bb5\u306a\u3069\u3067\u691c\u7d22\u3059\u308b\u3002\u307e\u305f\u3001\u5024\u6bb5\u3067\u4e26\u3073\u66ff\u3048\u3059\u308b\u3002
\n####\u2461\u672c\u306e\u7f6e\u304b\u308c\u3066\u3044\u308b\u5e97\u8217\u540d\u3092\u8868\u793a\u3055\u305b\u308b\u3002\u691c\u7d22\u3092\u5e97\u8217\u3054\u3068\u306e\u691c\u7d22\u3084\u3001\u5730\u57df\u3054\u3068\u3001\u90fd\u9053\u5e9c\u770c\u3054\u3068\u306e\u691c\u7d22\u304c\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u305f\u3044\u3002
\n####\u2462\u6771\u4eac\u306e\u5728\u5eab\u30bb\u30f3\u30bf\u30fc\u306b\u3042\u308b\u672c\u304c\u3042\u308b\u3002\u305d\u306e\u5834\u5408\u306f\u3001\u6240\u6709\u5e97\u30b3\u30fc\u30c9\u3092\u57fa\u306b\u5e97\u8217\u3092\u6307\u5b9a\u3059\u308b\u3002
\n####\u2463\u30bb\u30fc\u30eb\u30ad\u30e3\u30f3\u30da\u30fc\u30f3\u3092\u884c\u3063\u3066\u3044\u308b\u6642\u304c\u3042\u308b\u304c\u3001\u30ad\u30e3\u30f3\u30da\u30fc\u30f3\u4e2d\u306f\u30bb\u30fc\u30eb\u4fa1\u683c\u3092\u8868\u793a\u3057\u3001\u4e26\u3073\u66ff\u3048\u306f\u30bb\u30fc\u30eb\u4fa1\u683c\u3092\u57fa\u306b\u4e26\u3073\u66ff\u3048\u3092\u3059\u308b\u3002<\/p>\n

###\u6e96\u5099<\/del><\/p>\n

\u307e\u305a\u306f\u74b0\u5883\u3092\u4f5c\u308a\u307e\u3059\u3002
\n\u4e0b\u8a18\u306erails-api\u3092\u4f7f\u3063\u3066\u4f5c\u3063\u3066\u3044\u304d\u307e\u3059\u3002
\nhttps:\/\/github.com\/rails-api\/rails-api<\/a><\/p>\n

rails\u30a2\u30d7\u30ea\u4f5c\u6210<\/p>\n

$ rails-api new BookSearch<\/code><\/pre>\n

\u4e0b\u8a18\u30b5\u30a4\u30c8\u3092\u53c2\u8003\u306b\u3057\u3066\u9802\u304f\u3068\u3059\u3093\u306a\u308a\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u3067\u304d\u308b\u304b\u3068\u601d\u3044\u307e\u3059\u3002
\n
http:\/\/railscasts.com\/episodes\/348-the-rails-api-gem?language=ja&view=asciicast<\/a><\/p>\n

Gemfile<\/p>\n

gem 'mysql2'<\/code><\/pre>\n

config\/database.yml<\/p>\n

default: &default\n  adapter: mysql2\n  encoding: utf8\n  username: root\n  password: pw\n  host: 'localhost'\n  pool: 5\n  timeout: 5000\n  charset: utf8\n\ndevelopment:\n  <<: *default\n  database: dev_db\n\ntest:\n  <<: *default\n  database: test_db\n\nproduction:\n  <<: *default\n  database: pro_db<\/code><\/pre>\n

\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u4f5c\u6210\u3068model\u751f\u6210<\/p>\n

$ bundle install\n$ bundle exec rake db:create\n$ bundle exec rails g model book\n$ bundle exec rails g model shop\n$ bundle exec rails g model campaign\n$ bundle exec rails g model campaign_book<\/code><\/pre>\n

migration\u30d5\u30a1\u30a4\u30eb\u4fee\u6b63<\/p>\n

class CreateBooks < ActiveRecord::Migration\n  def change\n    create_table :books do |t|\n      t.string :title\n      t.string :author\n      t.string :publisher\n      t.integer :price\n      t.string :shop_id\n      t.string :stock_shop_id\n      t.timestamps\n    end\n  end\nend<\/code><\/pre>\n
class CreateShops < ActiveRecord::Migration\n  def change\n    create_table :shops do |t|\n      t.string :name\n      t.integer :prefecture\n      t.integer :area\n      t.timestamps\n    end\n  end\nend<\/code><\/pre>\n
class CreateCampaigns < ActiveRecord::Migration\n  def change\n    create_table :campaigns do |t|\n      t.string :title\n      t.date :start_date\n      t.date :end_date\n      t.timestamps\n    end\n  end\nend<\/code><\/pre>\n
class CreateCampaignBooks < ActiveRecord::Migration\n  def change\n    create_table :campaign_books do |t|\n      t.integer :campaign_id\n      t.integer :book_id\n      t.integer :sale_price\n      t.timestamps\n    end\n  end\nend<\/code><\/pre>\n

\u30de\u30a4\u30b0\u30ec\u30fc\u30c8<\/p>\n

$ bundle exec rake db:migrate<\/code><\/pre>\n

\u4ee5\u4e0a\u3067\u6e96\u5099\u304c\u51fa\u6765\u307e\u3057\u305f\u3002<\/p>\n

###\u2460\u672c\u306e\u30bf\u30a4\u30c8\u30eb\u3001\u8457\u8005\u540d\u3001\u51fa\u7248\u793e\u3001\u5024\u6bb5\u306a\u3069\u3067\u691c\u7d22\u3059\u308b\u3002\u307e\u305f\u3001\u5024\u6bb5\u3067\u4e26\u3073\u66ff\u3048\u3059\u308b\u3002
\n####\u30dd\u30a4\u30f3\u30c8\u30fb\u30fb\u30fb\u30fb\u30fbscope\u306e\u4e2d\u3067return if \u301c\u3092\u4f7f\u3046<\/p>\n

\u3053\u306e\u5b9f\u88c5\u3092\u884c\u3046\u6642\u306b\u3001\u6700\u521d\u306f\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u30fc\u306b\u6065\u305a\u304b\u3057\u306a\u304c\u3089\u4e0b\u8a18\u306e\u3088\u3046\u306a\u66f8\u304d\u65b9\u3092\u3057\u3066\u3057\u307e\u3063\u3066\u3044\u307e\u3057\u305f\u3002<\/p>\n

app\/controller\/api\/search_book_controller.rb<\/p>\n

module Api\n  class BookController < ApiController\n  def index\n    exec_query = 'Book'\n    exec_query += 'where("title = ?", params[:title])' if params[:title].present?\n    exec_query += 'where("author = ?", params[:author])' if params[:author].present?\n    exec_query += 'where("publisher = ?", params[:publisher])' if params[:publisher].present?\n    exec_query += 'where("price <= ?", params[:price])' if params[:price].present?\n    exec_query += 'order("price")' if params[:order].present? && params[:order] == 'price'\n\n    @books = eval(exec_query)\n    render\n  end\nend<\/code><\/pre>\n

\u30d1\u30e9\u30e1\u30fc\u30bf\u30fc\u304c\u3042\u308b\u6642\u306fexec_query\u306e\u6587\u5b57\u5217\u306e\u4e2d\u306b\u691c\u7d22\u6761\u4ef6\u3092\u7d44\u307f\u8fbc\u3093\u3067\u3044\u304d\u3001\u6700\u5f8c\u306beval\u3067\u5b9f\u884c\u3059\u308b\u3001\u3068\u3044\u3046\u3084\u308a\u65b9\u3067\u3059\u3002<\/p>\n

\u3057\u304b\u3057\u3001\u3053\u308c\u3067\u306f\u3059\u3054\u304f\u898b\u3065\u3089\u304f\u306a\u3063\u3066\u3057\u307e\u3044\u307e\u3059\u3057\u3001\u4eca\u5f8c\u3001\u6a5f\u80fd\u304c\u8ffd\u52a0\u3055\u308c\u308b\u6642\u3082\u3069\u3093\u3069\u3093\u5b9f\u88c5\u304c\u96e3\u3057\u304f\u306a\u3063\u3066\u3044\u304f\u3057\u3001\u4f55\u3088\u308a\u3082rails\u306e\u66f8\u304d\u65b9\u3067\u306f\u306a\u3044\u3001\u3068\u5049\u5927\u306a\u8af8\u5148\u8f29\u65b9\u306e\u6307\u6458\u3092\u53d7\u3051\u307e\u3057\u305f\u3002
\nscope\u3092\u4f7f\u3063\u3066\u5b9f\u88c5\u3057\u3066\u3044\u3063\u305f\u3089\u30b9\u30c3\u30ad\u30ea\u66f8\u3051\u308b\u3088\u3001\u3068\u3044\u3046\u3053\u3068\u3067\u3001scope\u3067\u66f8\u3044\u3066\u3044\u304f\u3053\u3068\u306b\u3057\u307e\u3057\u305f\u3002
\n\u30e2\u30c7\u30eb\u306b\u30ed\u30b8\u30c3\u30af\u3092\u66f8\u304f\u3068\u3001\u5225\u306e\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u30fc\u3067\u3082\u540c\u3058\u30ed\u30b8\u30c3\u30af\u3092\u4f7f\u3048\u308b\u3068\u3044\u3046\u30e1\u30ea\u30c3\u30c8\u306a\u3069\u3082\u3042\u308a\u307e\u3059\u3002
\n\u3068\u3044\u3046\u3053\u3068\u3067\u66f8\u3044\u3066\u307f\u305f\u306e\u304c\u3053\u3061\u3089\u3067\u3059\u3002<\/p>\n

app\/model\/book.rb<\/p>\n

class Book < ActiveRecord::Base\n  scope :serch_books, lambda { |params|\n    search_with_title(params[:title])\n    search_with_author(params[:author])\n    search_with_publisher(params[:publisher])\n    search_with_price(params[:price])\n    order_by_price(params[:order])\n  }\n\n  scope :search_with_title, lambda { |title|\n    return if title.nil?\n    where('title = ?', title)\n  }\n\n  scope :search_with_author, lambda { |author|\n    return if author.nil?\n    where('author = ?', author)\n  }\n\n  scope :search_with_publisher, lambda { |publisher|\n    return if publisher.nil?\n    where('publisher = ?', publisher)\n  }\n\n  scope :search_with_price, lambda { |price|\n    return if price.nil?\n    where('price <= ?', price)\n  }\n\n  scope :order_by_price, lambda { |order|\n    return if order.nil? && order == 'price'\n    order('price')\n  }\nend<\/code><\/pre>\n

app\/controller\/api\/search_book_controller.rb<\/p>\n

module Api\n  class SearchBookController < ApiController\n  def index\n    @books = Book.search_books(params)\n    render\n  end\nend<\/code><\/pre>\n

\u30e2\u30c7\u30eb\u5074\u306f\u5c11\u3057\u591a\u304f\u306a\u308a\u307e\u3057\u305f\u304c\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u30fc\u5074\u304c\u898b\u4e8b\u306b\u30b9\u30c3\u30ad\u30ea\u3057\u307e\u3057\u305f\u306d\u3002<\/p>\n

scope\u3067\u66f8\u3044\u305f\u6700\u5927\u306e\u30e1\u30ea\u30c3\u30c8\u306f\u30d1\u30e9\u30e1\u30fc\u30bf\u30fc\u304cnil\u306e\u6642\u306b\u3001\u51e6\u7406\u3092\u629c\u3051\u3066\u3001\u6b21\u306e\u51e6\u7406\u3092\u5b9f\u88c5\u3057\u3066\u304f\u308c\u308b\u3001\u3068\u3044\u3046\u3068\u3053\u308d\u306b\u3042\u308b\u3068\u601d\u3044\u307e\u3059\u3002
\n\u3053\u308c\u306flambda\u306e\u7279\u6027\u3060\u3068\u601d\u3044\u307e\u3059\u304c\u3001proc\u306e\u4e2d\u3067return\u3059\u308b\u3068\u3001\u30e1\u30bd\u30c3\u30c9\u5168\u4f53\u306e\u51e6\u7406\u304b\u3089\u629c\u3051\u3066\u3057\u307e\u3046\u306e\u306b\u5bfe\u3057\u3001lambda\u306f\u305d\u306e\u51e6\u7406\u304b\u3089\u629c\u3051\u3066\u3001\u6b21\u306e\u51e6\u7406\u3092\u5b9f\u884c\u3057\u306b\u3044\u304f\u3068\u3044\u3046\u7279\u6027\u304c\u3042\u308b\u306e\u3067\u3001\u3053\u3046\u3044\u3063\u305f\u3053\u3068\u304c\u5b9f\u73fe\u51fa\u6765\u3066\u3044\u307e\u3059\u3002
\n\u3053\u3061\u3089\u306e\u30da\u30fc\u30b8\u304c\u5206\u304b\u308a\u3084\u3059\u304b\u3063\u305f\u3067\u3059\u3002<\/p>\n

http:\/\/blog.livedoor.jp\/sasata299\/archives\/51488276.html<\/a><\/p>\n

lambda\u306f\u679c\u305f\u3057\u3066\u672c\u5f53\u306b\u305d\u3046\u306a\u3063\u3066\u3044\u308b\u306e\u304b\u3001\u30af\u30e9\u30b9\u30e1\u30bd\u30c3\u30c9\u3068\u6bd4\u8f03\u3057\u3066\u307f\u307e\u3057\u305f\u3002
\nscope\u3068\u540c\u3058\u5b9f\u88c5\u306e\u30af\u30e9\u30b9\u30e1\u30bd\u30c3\u30c9\u3092\u4f5c\u308a\u307e\u3059\u3002<\/p>\n

  def.self method_search_with_title\n    return if title.nil?\n    where('title = ?', title)\n  end<\/code><\/pre>\n

rails console\u3067\u5b9f\u884c\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n

2.1.2 :003 > Book.method_search_with_title('\u3088\u304f\u308f\u304b\u308bRuby on Rails')\n  Book Load (0.6ms)  SELECT `books`.* FROM `books`  WHERE (title = '\u3088\u304f\u308f\u304b\u308bRuby on Rails')\n => #<ActiveRecord::Relation [#<Book id: 1, title: "\u3088\u304f\u308f\u304b\u308bRuby on Rails", author: "\u5c71\u7530\u3000\u592a\u90ce", publisher: "\u5c71\u7530\u51fa\u7248", price: 2000, shop_id: 1, stock_shop_id: nil, created_at: nil, updated_at: nil>]><\/code><\/pre>\n

\u5024\u3092\u5165\u308c\u3066\u6e21\u3057\u305f\u6642\u306f\u3001\u610f\u56f3\u3057\u305f\u30af\u30a8\u30ea\u3092\u767a\u884c\u3057\u3001ActiveRecord::Relation\u304c\u8fd4\u3063\u3066\u304d\u307e\u3057\u305f\u3002
\n\u3067\u306fnil\u3092\u5165\u308c\u3066\u307f\u307e\u3059\u3002<\/p>\n

2.1.2 :005 > Book.method_search_with_title(nil)\n => nil\n2.1.2 :006 ><\/code><\/pre>\n

\u5f53\u305f\u308a\u524d\u3067\u3059\u304cnil\u304c\u8fd4\u3063\u3066\u304d\u307e\u3057\u305f\u3002<\/p>\n

scope\u3067\u540c\u3058\u3053\u3068\u3092\u3084\u3063\u3066\u307f\u307e\u3059\u3002<\/p>\n

2.1.2 :006 > Book.search_with_title('\u3088\u304f\u308f\u304b\u308bRuby on Rails')\n  Book Load (0.9ms)  SELECT `books`.* FROM `books`  WHERE (title = '\u3088\u304f\u308f\u304b\u308bRuby on Rails')\n => #<ActiveRecord::Relation [#<Book id: 1, title: "\u3088\u304f\u308f\u304b\u308bRuby on Rails", author: "\u5c71\u7530\u3000\u592a\u90ce", publisher: "\u5c71\u7530\u51fa\u7248", price: 2000, shop_id: 1, stock_shop_id: nil, created_at: nil, updated_at: nil>]><\/code><\/pre>\n

nil<\/p>\n

 2.1.2 :008 > Book.search_with_title(nil)\n  Book Load (0.3ms)  SELECT `books`.* FROM `books`\n => #<ActiveRecord::Relation [#<Book id: 1, title: "\u3088\u304f\u308f\u304b\u308bRuby on Rails", author: "\u5c71\u7530\u3000\u592a\u90ce", publisher: "\u5c71\u7530\u51fa\u7248", price: 2000, shop_id: 1, stock_shop_id: nil, created_at: nil, updated_at: nil>]><\/code><\/pre>\n

nil\u3067\u5b9f\u884c\u3059\u308b\u3068\u3001SELECT books<\/code>.* FROM books<\/code>\u304c\u5b9f\u884c\u3055\u308c\u3066\u3044\u307e\u3059\u306d\u3002
\n\u3053\u308c\u306f\u3001Book.all\u3068\u540c\u3058\u52d5\u4f5c\u3092\u3057\u3066\u3044\u307e\u3059\u3002
\nscope\u306fActiveRecord::Relation\u304c\u623b\u3063\u3066\u304d\u3066\u3044\u308b\u306e\u306b\u5bfe\u3057\u3001\u30e1\u30bd\u30c3\u30c9\u3067\u306fnil\u304c\u8fd4\u3063\u3066\u304d\u307e\u3057\u305f\u3002
\n\u3068\u3044\u3046\u3053\u3068\u306f\u30e1\u30bd\u30c3\u30c9\u306e\u5834\u5408\u306f\u30d1\u30e9\u30e1\u30fc\u30bf\u30fc\u304c\u5b58\u5728\u3057\u306a\u3044\u6642\u306e\u51e6\u7406\u3092\u3001\u30e1\u30bd\u30c3\u30c9\u30c1\u30a7\u30a4\u30f3\u3057\u3066\u306e\u5b9f\u88c5\u306f\u3067\u304d\u306a\u3044\u3001\u3068\u3044\u3046\u3053\u3068\u3067\u3059\u306d\u3002
\nnil\u306b\u5bfe\u3057\u3066\u5b9f\u884c\u3059\u308b\u3068no method\u30a8\u30e9\u30fc\u306b\u306a\u308a\u307e\u3059\u306e\u3067\u3002<\/p>\n

\u3068\u3044\u3046\u3053\u3068\u3067\u3001scope\u306f\u975e\u5e38\u306b\u6709\u7528\u3060\u3068\u3044\u3046\u3053\u3068\u304c\u5b9f\u8a3c\u3067\u304d\u305f\u304b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n

\u3061\u306a\u307f\u306b\u3067\u3059\u304c\u3001scope\u306e\u4e2d\u3067scope\u3092\u547c\u3073\u51fa\u3057\u305f\u308a\u3001\u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3073\u51fa\u3057\u305f\u308a\u304c\u51fa\u6765\u307e\u3059\u3002
\n\u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3073\u51fa\u3059\u6642\u306f\u3001\u305d\u306e\u30e1\u30bd\u30c3\u30c9\u306f\u30af\u30e9\u30b9\u30e1\u30bd\u30c3\u30c9\u3067\u306a\u3051\u308c\u3070\u306a\u308a\u307e\u305b\u3093\u3002
\nscope\u306e\u4e2d\u3067\u6700\u7d42\u7684\u306bActiveRecord::Relation\u3092\u8fd4\u3059\u3088\u3046\u306b\u3059\u308c\u3070\u5927\u4e08\u592b\u3067\u3059\u3002<\/p>\n

\u3053\u308c\u3067\u3001\u2460\u306e\u5b9f\u88c5\u304c\u51fa\u6765\u307e\u3057\u305f\u3002<\/p>\n

###\u2461\u672c\u306e\u7f6e\u304b\u308c\u3066\u3044\u308b\u5e97\u8217\u540d\u3092\u8868\u793a\u3055\u305b\u308b\u3002\u691c\u7d22\u3092\u5e97\u8217\u3054\u3068\u306e\u691c\u7d22\u3084\u3001\u5730\u57df\u3054\u3068\u3001\u90fd\u9053\u5e9c\u770c\u3054\u3068\u306e\u691c\u7d22\u304c\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u305f\u3044
\n\u30dd\u30a4\u30f3\u30c8\u30fb\u30fb\u30fb\u30fb\u30fbleft join\u3092\u4f7f\u3046<\/p>\n

MySQL\u306b\u306fleft join\u3084right join\u3001inner join\u306a\u3069\u304c\u3042\u308a\u307e\u3059\u304c\u3001\u4eca\u56de\u306fleft join\u3067\u3059\u3002
\n\u672c\u6765\u306fhas_many\u3084belongs_to\u306a\u3069\u3067\u95a2\u9023\u4ed8\u3051\u3057\u3066\u3001include\u3001predecate\u3067\u5b9f\u88c5\u3057\u3066\u3044\u304f\u306e\u304crails\u306e\u738b\u9053\u3060\u3068\u601d\u3044\u307e\u3059\u304c\u3001\u4eca\u56de\u306eDB\u306fMySQL\u9650\u5b9a\u3060\u3068\u3044\u3046\u3053\u3068\u3084\u3001\u3053\u306e\u5f8c\u306e\u8907\u96d1\u306a\u30af\u30a8\u30ea\u306b\u5bfe\u5fdc\u3059\u308b\u3001\u3068\u3044\u3046\u4e8b\u3082\u8003\u3048\u3066joins\u3092\u4f7f\u3063\u3066\u5b9f\u88c5\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n

\u3053\u3053\u306f\u305d\u308c\u307b\u3069\u96e3\u3057\u304f\u306a\u306a\u3044\u306e\u3067\u3059\u304c\u3001\u5fc5\u8981\u306a\u30ab\u30e9\u30e0\u3092select\u3057\u3066\u3001joins\u3067left join\u3092sql\u6587\u3067\u66f8\u304f\u3060\u3051\u3067\u3059\u3002<\/p>\n

app\/model\/book.rb<\/p>\n

  scope :select_columns, lambda {\n    select('books.*, shops.name as shop_name')\n  }\n\n  scope :join_tables, lambda {\n    joins('left join shops\n            on shops.id =\n            books.shop_id')\n  }\n\n  scope :search_with_shop, lambda { |shop|\n    return if shop.nil?\n    where('shop_id = ?', shop)\n  }\n\n  scope :search_with_area, lambda { |area|\n    return if area.nil?\n    where('shops.area = ?', area)\n  }\n\n  scope :search_with_pref, lambda { |pref|\n    return if pref.nil?\n    where('shops.prefecture = ?', pref)\n  }<\/code><\/pre>\n

\u305d\u3057\u3066scope :search_books\u306e\u4e2d\u306b\u4e0a\u8a18scope\u3092\u542b\u3081\u307e\u3059\u3002
\n\u3053\u308c\u3067\u5e97\u8217\u540d\u3092\u8868\u793a\u3055\u305b\u305f\u308a\u3001\u5e97\u8217\u3084\u30a8\u30ea\u30a2\u3084\u90fd\u9053\u5e9c\u770c\u3092\u6307\u5b9a\u3057\u3066\u306e\u691c\u7d22\u304c\u51fa\u6765\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3057\u305f\u3002<\/p>\n

\u30b3\u30f3\u30bd\u30fc\u30eb\u3067\u8a66\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n

\u30d1\u30e9\u30e0\u4f5c\u6210<\/p>\n

2.1.2 :001 > params = {pref: 1}\n => {:pref=>1}<\/code><\/pre>\n

books\u306b\u7d50\u679c\u3092\u4ee3\u5165<\/p>\n

2.1.2 :004 > books = Book.search_books(params)\n  Book Load (0.9ms)  SELECT books.*, shops.name as shop_name FROM `books` left join shops\n             on shops.id =\n             books.shop_id WHERE (shops.prefecture = 1)\n => #<ActiveRecord::Relation [#<Book id: 1, title: "\u3088\u304f\u308f\u304b\u308bRuby on Rails", author: "\u5c71\u7530\u3000\u592a\u90ce", publisher: "\u5c71\u7530\u51fa\u7248", price: 2000, shop_id: 1, stock_shop_id: nil, created_at: nil, updated_at: nil>]><\/code><\/pre>\n

shop_name\u3067\u5e97\u8217\u540d\u3092\u53d6\u5f97<\/p>\n

2.1.2 :005 > books[0].shop_name\n => "\u672c\u5c4b\u3000\u6771\u4eac\u5e97"<\/code><\/pre>\n

\u52d5\u7684\u306bActiveRecord::Relation\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u30e1\u30f3\u30d0\u3092\u8ffd\u52a0\u3057\u3066\u304f\u308c\u308b\u306e\u3067\u3001\u975e\u5e38\u306b\u4fbf\u5229\u3067\u3059\u306d\u3002<\/p>\n

###\u2462\u6771\u4eac\u306e\u5728\u5eab\u30bb\u30f3\u30bf\u30fc\u306b\u3042\u308b\u672c\u304c\u3042\u308b\u3002\u305d\u306e\u5834\u5408\u306f\u3001\u6240\u6709\u5e97\u30b3\u30fc\u30c9\u3092\u57fa\u306b\u5e97\u8217\u3092\u6307\u5b9a\u3059\u308b\u3002
\n####\u30dd\u30a4\u30f3\u30c8\u30fb\u30fb\u30fb\u30fb\u30fbMySQL\u306eif\u6587\u3092\u4f7f\u3046
\n\u3061\u3087\u3063\u3068\u8981\u4ef6\u304c\u8aac\u660e\u4e0d\u8db3\u3067\u3057\u305f\u304c\u3001\u524d\u63d0\u3068\u3057\u3066\u3001books\u306eshop_id\u30ab\u30e9\u30e0\u304c0\u306e\u6642\u3001\u305d\u306e\u672c\u306f\u672c\u90e8\u306e\u5728\u5eab\u306b\u3042\u308b\u306e\u3067\u3001books\u306estock_shop_id\u3092\u57fa\u306b\u3001shop\u30c6\u30fc\u30d6\u30eb\u3068\u7d10\u3065\u3051\u308b\u3001\u3068\u3044\u3046\u3053\u3068\u3067\u3059\u3002
\n\u60f3\u5b9a\u3068\u3057\u3066\u306f\u3001\u305d\u306e\u672c\u306e\u6240\u6709\u306f\u5e97\u8217\u306b\u3042\u308b\u304c\u3001\u672c\u90e8\u304b\u3089\u307e\u3060\u672a\u767a\u9001\u306e\u5834\u5408\u306a\u3069\u3001\u3067\u3059\u3002<\/p>\n

\u3053\u3053\u306b\u306f\u3061\u3087\u3063\u3068\u30cf\u30de\u308a\u307e\u3057\u305f\u3002
\n\u6700\u521d\u306funion all\u3092\u4f7f\u304a\u3046\u3068\u601d\u3063\u3066\u304a\u308a\u307e\u3057\u305f\u3002
\nsql\u6587\u306f\u4e0b\u8a18\u306e\u3088\u3046\u306asql\u6587\u3067\u3059\u3002<\/p>\n

mysql> select books.*,shops.name as shop_name from books left join shops on books.shop_id = shops.id where shops.id is not null\n    -> union all\n    -> select books.*,shops.name as shop_name from books left join shops on books.stock_shop_id = shops.id where shops.id is not null;\n+----+------------------------------+-----------------+--------------+-------+---------+---------------+------------+------------+--------------------+\n| id | title                        | author          | publisher    | price | shop_id | stock_shop_id | created_at | updated_at | shop_name          |\n+----+------------------------------+-----------------+--------------+-------+---------+---------------+------------+------------+--------------------+\n|  1 | \u3088\u304f\u308f\u304b\u308bRuby on Rails      | \u5c71\u7530\u3000\u592a\u90ce      | \u5c71\u7530\u51fa\u7248     |  2000 |       1 |          NULL | NULL       | NULL       | \u672c\u5c4b\u3000\u6771\u4eac\u5e97       |\n|  2 | \u3088\u304f\u5206\u304b\u308bSQL                | \u7530\u4e2d\u3000\u6b21\u90ce      | \u7530\u4e2d\u6587\u5eab     |  3000 |       0 |             1 | NULL       | NULL       | \u672c\u5c4b\u3000\u6771\u4eac\u5e97       |\n+----+------------------------------+-----------------+--------------+-------+---------+---------------+------------+------------+--------------------+\n2 rows in set (0.01 sec)<\/code><\/pre>\n

sql\u3060\u3068\u554f\u984c\u306a\u304f\u5b9f\u884c\u3067\u304d\u3001\u671f\u5f85\u901a\u308a\u306e\u7d50\u679c\u304c\u8fd4\u3063\u3066\u304d\u307e\u3059\u3002
\n\u554f\u984c\u306frails\u3067\u5b9f\u88c5\u3059\u308b\u6642\u3067\u3057\u305f\u3002<\/p>\n

\u3069\u3046\u3084\u3063\u3066\u3082ActiveRelation\u306e\u578b\u3067\u8fd4\u3063\u3066\u304d\u307e\u305b\u3093\u3002
\n\u3053\u308c\u306f\u5b9f\u88c5\u5168\u4f53\u3092\u898b\u76f4\u3055\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u306e\u304b\uff1f\u3068\u3042\u304d\u3089\u3081\u304b\u3051\u307e\u3057\u305f\u304c\u3001sql\u306eif\u6587\u3067\u5b9f\u88c5\u51fa\u6765\u308b\u304b\u3082\u3068\u601d\u3044\u8a66\u3057\u3066\u307f\u305f\u3089\u5b9f\u88c5\u3067\u304d\u307e\u3057\u305f\u3002<\/p>\n

  scope :join_tables, lambda {\n    joins('left join shops\n            on shops.id =\n            if(books.shop_id = 0,\n            books.stock_shop_id,\n            books.shop_id)')\n  }<\/code><\/pre>\n

rails console\u3067\u8a66\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n

2.1.2 :001 > books = Book.search_books({})\n  Book Load (3.4ms)  SELECT books.*, shops.name as shop_name FROM `books` left join shops\n             on shops.id =\n             if(books.shop_id = 0,\n             books.stock_shop_id,\n             books.shop_id)\n => #<ActiveRecord::Relation [#<Book id: 1, title: "\u3088\u304f\u308f\u304b\u308bRuby on Rails", author: "\u5c71\u7530\u3000\u592a\u90ce", publisher: "\u5c71\u7530\u51fa\u7248", price: 2000, shop_id: 1, stock_shop_id: nil, created_at: nil, updated_at: nil>, #<Book id: 2, title: "\u3088\u304f\u5206\u304b\u308bSQL", author: "\u7530\u4e2d\u3000\u6b21\u90ce", publisher: "\u7530\u4e2d\u6587\u5eab", price: 3000, shop_id: 0, stock_shop_id: 1, created_at: nil, updated_at: nil>]>\n2.1.2 :002 > books[0].title\n => "\u3088\u304f\u308f\u304b\u308bRuby on Rails"\n2.1.2 :003 > books[0].shop_name\n => "\u672c\u5c4b\u3000\u6771\u4eac\u5e97"\n2.1.2 :004 > books[1].title\n => "\u3088\u304f\u5206\u304b\u308bSQL"\n2.1.2 :005 > books[1].shop_name\n => "\u672c\u5c4b\u3000\u6771\u4eac\u5e97"<\/code><\/pre>\n

union\u3067\u7e4b\u3044\u3060\u6642\u3068\u540c\u3058\u3088\u3046\u306b\u53d6\u5f97\u51fa\u6765\u3066\u3044\u308b\u3053\u3068\u304c\u78ba\u8a8d\u51fa\u6765\u307e\u3057\u305f\u3002<\/p>\n

sql\u306eunion\u6587\u306f\u3001\u3064\u306a\u3050\u5206\u3060\u3051\u30af\u30a8\u30ea\u3092\u767a\u884c\u3059\u308b\u3053\u3068\u306b\u306a\u308b\u306e\u3067\u3001\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u7684\u306b\u3082if\u6587\u3067\u5b9f\u88c5\u3059\u308b\u307b\u3046\u304c\u512a\u308c\u3066\u3044\u308b\u306e\u3067\u7d50\u679c\u7684\u306b\u306f\u826f\u304b\u3063\u305f\u3067\u3059\u3002<\/p>\n

\u4ed6\u306e\u5834\u9762\u3067\u3082sql\u306eif\u6587\u306f\u7d50\u69cb\u4f7f\u3048\u3066\u7d04\u306b\u7acb\u3061\u307e\u3059\u3002<\/p>\n

###\u2463\u30bb\u30fc\u30eb\u30ad\u30e3\u30f3\u30da\u30fc\u30f3\u3092\u884c\u3063\u3066\u3044\u308b\u6642\u304c\u3042\u308b\u304c\u3001\u30ad\u30e3\u30f3\u30da\u30fc\u30f3\u4e2d\u306f\u30bb\u30fc\u30eb\u4fa1\u683c\u3092\u8868\u793a\u3057\u3001\u4e26\u3073\u66ff\u3048\u306f\u30bb\u30fc\u30eb\u4fa1\u683c\u3092\u57fa\u306b\u4e26\u3073\u66ff\u3048\u3092\u3059\u308b\u3002
\n####\u30dd\u30a4\u30f3\u30c8(\u3068\u3086\u3046\u304b\u529b\u6280)\u30fb\u30fb\u30fbsql\u3092\u57cb\u3081\u8fbc\u3080<\/p>\n

\u3053\u308c\u306f\u4eca\u307e\u3067\u306e\u8907\u5408\u6280\u306e\u3088\u3046\u306a\u3053\u3068\u3092\u3057\u307e\u3059\u3002
\n\u4e0b\u8a18\u306e3\u3064\u306e\u624b\u9806\u306b\u5206\u3051\u3066\u5b9f\u88c5\u3057\u307e\u3057\u305f\u3002
\n#####\uff11\u3001\u73fe\u5728\u958b\u50ac\u4e2d\u306e\u3059\u3079\u3066\u306e\u30ad\u30e3\u30f3\u30da\u30fc\u30f3\u306eid\u3092\u30d4\u30c3\u30af\u30a2\u30c3\u30d7\u3059\u308b
\n#####\uff12\u3001\u30d4\u30c3\u30af\u30a2\u30c3\u30d7\u3057\u305f\u30ad\u30e3\u30f3\u30da\u30fc\u30f3\u306b\u3001books\u30e2\u30c7\u30eb\u3092\u7d50\u3073\u3064\u3051\u3066\u3001\u30ad\u30e3\u30f3\u30da\u30fc\u30f3\u304c\u3042\u308b\u6642\u306f\u30ad\u30e3\u30f3\u30da\u30fc\u30f3\u4fa1\u683c\u3092\u3001\u7121\u3044\u6642\u306f\u901a\u5e38\u306e\u4fa1\u683c\u3092\u53d6\u5f97\u3059\u308b\u30ab\u30e9\u30e0\u3092\u4f5c\u308b\u3002
\n#####\uff13\u3001\u305d\u306e\u30ab\u30e9\u30e0\u3067order\u3092\u3059\u308b<\/p>\n

\u6700\u7d42\u7684\u306b\u5b8c\u6210\u3057\u305f\u30b3\u30fc\u30c9\u306f\u4e0b\u8a18\u306e\u901a\u308a\u3067\u3059\u3002<\/p>\n

app\/model\/book.rb<\/p>\n

class Book < ActiveRecord::Base\n  scope :serch_books, lambda { |params|\n    select_columns\n    .join_tables\n    .search_with_title(params[:title])\n    .search_with_author(params[:author])\n    .search_with_publisher(params[:publisher])\n    .less_than_price(params[:min_price])\n    .greater_than_price(params[:max_price])\n    .search_with_shop(params[:shop])\n    .search_with_area(params[:area])\n    .search_with_pref(params[:pref])\n    .order_by_price(params[:order])\n  }\n\n  scope :select_columns, lambda {\n    campaign_ids = Campaign.where('? between start_date and end_date', Time.now).pluck(:id)\n    select("books.*,\n            shops.name as shop_name,\n            if((campaign_books.sale_price is not null) and (campaign_books.campaign_id in (\n            #{campaign_ids.empty? ? '0' : campaign_ids.join(',')}\n            )), campaign_books.sale_price, books.price) as least_price")\n  }\n\n  scope :join_tables, lambda {\n    joins('left join shops\n            on shops.id =\n            if(books.shop_id = 0,\n             books.stock_shop_id,\n             books.shop_id)')\n    .joins('left join campaign_books\n            on campaign_books.book_id =\n            books.id')\n  }\n\n  scope :search_with_title, lambda { |title|\n    return if title.nil?\n    where('title in (?)', title)\n  }\n\n  scope :search_with_author, lambda { |author|\n    return if author.nil?\n    where('author in (?)', author)\n  }\n\n  scope :search_with_publisher, lambda { |publisher|\n    return if publisher.nil?\n    where('publisher in (?)', publisher)\n  }\n\n  scope :less_than_price, lambda { |price|\n    return if price.nil?\n    where('price >= ?', price)\n  }\n\n  scope :greater_than_price, lambda { |price|\n    return if price.nil?\n    where('price <= ?', price)\n  }\n\n  scope :order_by_price, lambda { |order|\n    return if order.nil? && order == 'price'\n    order('least_price')\n  }\n\n  scope :search_with_shop, lambda { |shop|\n    return if shop.nil?\n    where('shop_id in (?)', shop)\n  }\n\n  scope :search_with_area, lambda { |area|\n    return if area.nil?\n    where('shops.area in (?)', area)\n  }\n\n  scope :search_with_pref, lambda { |pref|\n    return if pref.nil?\n    where('shops.prefecture in (?)', pref)\n  }\nend<\/code><\/pre>\n

\u3064\u3044\u3067\u306b\u4ed6\u306e\u30ab\u30e9\u30e0\u3082\u914d\u5217\u3067\u691c\u7d22\u51fa\u6765\u308b\u3088\u3046\u306b\u3057\u307e\u3057\u305f\u3002<\/p>\n

###\u611f\u60f3<\/p>\n

\u3082\u3063\u3068\u30b9\u30de\u30fc\u30c8\u306a\u3084\u308a\u65b9\u306a\u3069\u3082\u3042\u308b\u306e\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002
\n\u3082\u3057\u826f\u3044\u30ce\u30a6\u30cf\u30a6\u306a\u3069\u304c\u3054\u3056\u3044\u307e\u3057\u305f\u3089\u662f\u975e\u304a\u6559\u3048\u4e0b\u3055\u3044\u307e\u305b\u3002
\n\u3067\u306f\u826f\u304dRails Life\u3092\u3002<\/p>\n

Ruby on Rails\u3092\u6d3b\u7528\u3057\u305fWeb\u30b5\u30fc\u30d3\u30b9\u3084\u696d\u52d9\u30b7\u30b9\u30c6\u30e0\u958b\u767a<\/a>\u3092\u3054\u691c\u8a0e\u306e\u4f01\u696d\u69d8\u306f\u3001\u662f\u975eMMM\u306b\u3054\u76f8\u8ac7\u4e0b\u3055\u3044\u307e\u305b\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"

\u6700\u8fd1\u793e\u5185\u306e\u798f\u5229\u539a\u751f\u3067\u3001\u300c\u5e74\u959310\u4e07\u5186\u3092\u4e0a\u9650\u3068\u3057\u3001\u696d\u52d9\u4e0a\u5fc5\u8981\u3001\u958b\u767a\u74b0\u5883\u306e\u52b9\u7387\u5316\u3084\u65b0\u3057\u3044\u77e5\u8b58\u7fd2\u5f97\u306b\u5f79\u7acb\u3064\u3068\u8003\u3048\u3089\u308c\u308b\u96fb\u5b50\u6a5f\u5668\u3084\u30b0\u30c3\u30ba\u306e\u8cfc\u5165\u304c\u53ef\u80fd\u300d\u3068\u3044\u3046\u3010\u696d\u52d9\u52b9\u7387\u5316\u5468\u8fba\u30b0\u30c3\u30ba\u8cfc\u5165\u88dc\u52a9\u3011\u306a\u308b\u3082\u306e\u304c\u767a\u8db3\u3057\u3001\u3055\u3063\u305d\u304f\u591c\u306a\u591c\u306aAmazon\u3067\u7f8e\u5473\u3057\u305d\u3046\u306aIT\u30ac\u30b8\u30a7\u30c3\u30c8\u304c\u306a\u3044\u304b\u63a2\u7d22\u3057\u3066\u3044\u308b\u524d\u7530\u3067\u3059\u3002 \u5f0a\u793e\u3067\u306f\u73fe\u5728\u30d5\u30ed\u30f3\u30c8\u30b5\u30a4\u30c9\u306fAngularJS\u3001\u30b5\u30fc\u30d0\u30fc\u30b5\u30a4\u30c9\u306frails\u3067API\u3068CMS\u3001\u3068\u3044\u3046\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3 […]<\/p>\n","protected":false},"author":1,"featured_media":826,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[9],"tags":[42],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts\/127"}],"collection":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/comments?post=127"}],"version-history":[{"count":0,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts\/127\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/media\/826"}],"wp:attachment":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/media?parent=127"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/categories?post=127"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/tags?post=127"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}