{"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
### \u307e\u305a\u306f\u74b0\u5883\u3092\u4f5c\u308a\u307e\u3059\u3002 rails\u30a2\u30d7\u30ea\u4f5c\u6210<\/p>\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 Gemfile<\/p>\n config\/database.yml<\/p>\n \u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u4f5c\u6210\u3068model\u751f\u6210<\/p>\n migration\u30d5\u30a1\u30a4\u30eb\u4fee\u6b63<\/p>\n \u30de\u30a4\u30b0\u30ec\u30fc\u30c8<\/p>\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 \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 \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 app\/model\/book.rb<\/p>\n app\/controller\/api\/search_book_controller.rb<\/p>\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\u6e96\u5099<\/del><\/p>\n
\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-api new BookSearch<\/code><\/pre>\n
\nhttp:\/\/railscasts.com\/episodes\/348-the-rails-api-gem?language=ja&view=asciicast<\/a><\/p>\ngem 'mysql2'<\/code><\/pre>\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
$ 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
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
$ bundle exec rake db:migrate<\/code><\/pre>\n
\n####\u30dd\u30a4\u30f3\u30c8\u30fb\u30fb\u30fb\u30fb\u30fbscope\u306e\u4e2d\u3067return if \u301c\u3092\u4f7f\u3046<\/p>\nmodule 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
\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>\nclass 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
module Api\n class SearchBookController < ApiController\n def index\n @books = Book.search_books(params)\n render\n end\nend<\/code><\/pre>\n
\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