{"id":1566,"date":"2015-06-02T20:50:06","date_gmt":"2015-06-02T11:50:06","guid":{"rendered":"http:\/\/www.prime-architect.co.jp\/myblog\/?p=1566"},"modified":"2015-06-02T21:16:15","modified_gmt":"2015-06-02T12:16:15","slug":"ruby-on-rails%e3%81%a7%e8%a4%87%e6%95%b0%e3%81%ae%e3%83%a2%e3%83%87%e3%83%ab%e3%81%8c%e7%b5%a1%e3%82%80%e6%a4%9c%e7%b4%a2%e7%94%bb%e9%9d%a2%e3%81%ae%e4%bd%9c%e3%82%8a%e6%96%b9","status":"publish","type":"post","link":"https:\/\/www.prime-architect.co.jp\/myblog\/ruby-on-rails-1566","title":{"rendered":"Ruby on Rails\u3067\u8907\u6570\u306e\u30e2\u30c7\u30eb\u304c\u7d61\u3080\u691c\u7d22\u753b\u9762\u306e\u4f5c\u308a\u65b9"},"content":{"rendered":"<p>\u3055\u3066\u3055\u3066\u3001\u4eca\u56de\u3082Ruby on Rails\u30cd\u30bf\u3067\u3059\u3002<\/p>\n<p>\u5927\u62b5\u306e\u696d\u52d9\u30b7\u30b9\u30c6\u30e0\u3067\u306f\u305f\u3044\u3066\u3044\u30c7\u30fc\u30bf\u3092\u691c\u7d22\u3059\u308b\u753b\u9762\u304c\u3042\u308a\u307e\u3059\u3002\u305d\u3093\u306a\u691c\u7d22\u753b\u9762\u3067\u3059\u304c\u3001\u7c21\u5358\u306a\u691c\u7d22\u6761\u4ef6\u3067\u3042\u308c\u3070\u3044\u3044\u306e\u3067\u3059\u304c\u3001<\/p>\n<p>\u5927\u62b5\u306f\u8907\u6570\u306e\u30e2\u30c7\u30eb\u304c\u7d61\u307f\u3001\u30a2\u30bd\u30b7\u30a8\u30fc\u30b7\u30e7\u30f3\u5148\u306e\u9805\u76ee\u3092\u691c\u7d22\u6761\u4ef6\u306b\u3057\u305f\u3044\u5834\u5408\u306a\u3069\u3001\u8907\u96d1\u306b\u306a\u308b\u30b1\u30fc\u30b9\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>Rails\u3084\u308a\u59cb\u3081\u306e\u9803\u3001\u304b\u306a\u308a\u30cf\u30de\u308a\u307e\u3057\u305f\u3002<\/p>\n<p>\u8907\u6570\u306e\u30e2\u30c7\u30eb\u304b\u3089\u306a\u308b\u30c7\u30fc\u30bf\u3092\u691c\u7d22\u3059\u308b\u969b\u3001\u305d\u3082\u305d\u3082\u3069\u3053\u306b\u691c\u7d22\u6761\u4ef6\u30ed\u30b8\u30c3\u30af\u3092\u66f8\u304f\u306e\u304b\u3001controller\u306b\u30ed\u30b8\u30c3\u30af\u3092\u66f8\u304f\u306e\u306f\u5fae\u5999\u3060\u3057\u3001<\/p>\n<p>\u304b\u3068\u8a00\u3063\u3066\u4e00\u3064\u306e\u30e2\u30c7\u30eb\u306b\u30e1\u30bd\u30c3\u30c9\u3092\u5b9a\u7fa9\u3057\u3066\u66f8\u304f\u306e\u3082\u306a\u3093\u3060\u304b\u306a\u301c\u3002\u306a\u3093\u3066\u601d\u3063\u305f\u308a\u3057\u3066\u3044\u307e\u3057\u305f\u3002\u3058\u3083\u3042\u3069\u3046\u3059\u3093\u306e\u3063\u3066\u611f\u3058\u306a\u306e\u3067\u3059\u304c\u3001<\/p>\n<p>\u306a\u304b\u306a\u304b\u305d\u3046\u3044\u3063\u305f\u60c5\u5831\u306f\u30cd\u30c3\u30c8\u306b\u306f\u51fa\u3066\u3044\u306a\u3044\u306e\u304c\u73fe\u72b6\u3067\u3059\u3002\u5f53\u521d\u306fransack\u3068\u3044\u3046gem\u3092\u5165\u308c\u3066\u958b\u767a\u3092\u9032\u3081\u3066\u3044\u305f\u306e\u3067\u3059\u304c\u3001<\/p>\n<p>\u3061\u3087\u3063\u3068\u8907\u96d1\u306a\u691c\u7d22\u6761\u4ef6\u3092\u3064\u304f\u308d\u3046\u3068\u3059\u308b\u3068\u3001\u3046\u307e\u304f\u4f7f\u3046\u3053\u3068\u304c\u51fa\u6765\u305a\u3001\u7d50\u5c40\u3084\u3081\u307e\u3057\u305f\u3002<\/p>\n<p>\u3067\u305f\u3069\u308a\u7740\u3044\u305f\u5148\u304c\u4ee5\u4e0b\u306e\u65b9\u6cd5\u3067\u3059\u3002\u3061\u306a\u307f\u306b\u3053\u308c\u306f\u300c\u5b9f\u8df5Ruby on Rails4\u300d\u306e\u672c\u3067\u66f8\u304b\u308c\u3066\u3044\u305f\u65b9\u6cd5\u3067\u3059\u3002\u3053\u308c\u304c\u4e00\u756a\u3057\u3063\u304f\u308a\u6765\u308b\u611f\u3058\u3067\u3059\u3002<\/p>\n<p>\u4f8b\u3068\u3057\u3066\u3001\u9867\u5ba2\u60c5\u5831\u4e00\u89a7\u3092\u8868\u793a\u3059\u308b\u51e6\u7406\u3067\u3001\u9867\u5ba2ID,\u540d\u524d\u3001TEL\u3067\u691c\u7d22\u3059\u308b\u753b\u9762\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<br \/>\n\u30e2\u30c7\u30eb\u306f<br \/>\nCustomer\u3068Address\u304c\u3042\u308a\u3001<br \/>\ncustomer  has_one address\u3000\u3068\u3044\u3063\u305f\u30a2\u30bd\u30b7\u30a8\u30fc\u30b7\u30e7\u30f3\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<br \/>\ntel\u306faddress\u30e2\u30c7\u30eb\u306e\u30d5\u30a3\u30fc\u30eb\u30c9\u3068\u3057\u307e\u3059\u3002<\/p>\n<p>\u307e\u305a\u306f\u3001form\u30af\u30e9\u30b9\u7528\u610f\u3057\u307e\u3059\u3002<br \/>\n\u3053\u306e\u30af\u30e9\u30b9\u306f<br \/>\nforms\u30d5\u30a9\u30eb\u30c0\u3092app\u914d\u4e0b\u306b\u4f5c\u6210\u3057\u3001\u305d\u306e\u4e2d\u306bform\u30af\u30e9\u30b9\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<br \/>\n\u305d\u3057\u3066\u3001ActiveModel\u3092include\u3059\u308b\u3053\u3068\u3067Model\u3068\u540c\u3058\u3088\u3046\u306a\u52d5\u304d\u3092\u3057\u3066\u304f\u308c\u307e\u3059\u3002<br \/>\n\u691c\u7d22\u6761\u4ef6\u306b\u6307\u5b9a\u3057\u305f\u3044\u9805\u76ee\u306fattr_accessor\u306b\u6307\u5b9a\u3057\u307e\u3059\u3002<\/p>\n<p>app\/forms<br \/>\ncustomer_search_form.rb<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">\r\nclass CustomerSearchForm\r\n  include ActiveModel::Model\r\n\r\n  attr_accessor \u00a0:id, :name, :tel\r\n\r\n  def search\r\n    rel = Customer\r\n\r\n    rel = rel.where(id: id) if id.present?\r\n\r\n    rel = rel.where(name: name) if name.present?\r\n\r\n    rel = rel.joins(:address).where(\u201caddress.tel&quot; =&amp;gt; tel) if tel.present?\r\n\r\n    rel\r\n  end\r\nend\r\n<\/pre>\n<p>\u7d9a\u3044\u3066\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u306f\u3053\u3093\u306a\u611f\u3058\u3067\u3059\u3002<br \/>\n\u5148\u307b\u3069\u4f5c\u6210\u3057\u305fForm\u30af\u30e9\u30b9\u3092new\u3059\u308b\u969b\u306b\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u5168\u3066\u30af\u30e9\u30b9\u306b\u6e21\u3057\u307e\u3059\u3002<br \/>\n\u305d\u3057\u3066search\u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3076\u3060\u3051\u3067\u3059\u3002<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">\r\ndef index\r\n\u3000\u3000@search_form = CustomerSearchForm.new(params[:search])\r\n\u3000\u3000@customers = @search_form.search\r\nend\r\n<\/pre>\n<p>view\u306f\u3053\u3093\u306a\u611f\u3058\u3067\u66f8\u304d\u307e\u3059\u3002<br \/>\n\u30dd\u30a4\u30f3\u30c8\u306fform\u306emethod\u3092get\u306b\u3059\u308b\u306e\u304c\u30dd\u30a4\u30f3\u30c8\u3067\u3059\u3002<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">\r\n= form_for @search_form, as: 'search', url: :customers, html: {method: :get} do |f|\r\n\r\n\u3000= f.text_field :id\r\n\u3000= f.text_field :name\r\n\u3000= f.text_field :tel\r\n\r\n  = f.submit &quot;\u691c\u7d22&quot;\r\n<\/pre>\n<p>\u3053\u306e\u3088\u3046\u306b\u3059\u308b\u3053\u3068\u3067\u3001\u81ea\u5728\u306b\u691c\u7d22\u6761\u4ef6\u3092\u4f5c\u308b\u3053\u3068\u304c\u51fa\u6765\u307e\u3059\u3002<br \/>\n\u8907\u6570\u306e\u30e2\u30c7\u30eb\u3092\u51e6\u7406\u3059\u308b\u5834\u5408\u3001form\u30af\u30e9\u30b9\u3092\u4f5c\u6210\u3059\u308b\u65b9\u6cd5\u306f\u306a\u304b\u306a\u304b\u3044\u3044\u30d7\u30e9\u30af\u30c6\u30a3\u30b9\u306e\u3088\u3046\u3067\u3059\u3002<\/p>\n<p>\u3053\u306e\u624b\u6cd5\u306f\u3053\u3061\u3089\u306e\u672c\u3092\u53c2\u8003\u306b\u3057\u3066\u3044\u307e\u3059\u3002<br \/>\n\u4ed6\u306b\u3082rails\u306e\u8a2d\u8a08\u65b9\u6cd5\u306b\u3064\u3044\u3066\u3001\u8272\u3005\u3068\u8a73\u3057\u304f\u8f09\u3063\u3066\u3044\u307e\u3059\u306e\u3067\u3001\u8208\u5473\u3042\u308b\u65b9\u306f\u662f\u975e<br \/>\n<iframe src=\"http:\/\/rcm-fe.amazon-adsystem.com\/e\/cm?t=primearchitec-22&#038;o=9&#038;p=8&#038;l=as1&#038;asins=B00LBPDNSY&#038;ref=qf_sp_asin_til&#038;fc1=000000&#038;IS2=1&#038;lt1=_blank&#038;m=amazon&#038;lc1=0000FF&#038;bc1=000000&#038;bg1=FFFFFF&#038;npa=1&#038;f=ifr\" style=\"width:120px;height:240px;\" scrolling=\"no\" marginwidth=\"0\" marginheight=\"0\" frameborder=\"0\"><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3055\u3066\u3055\u3066\u3001\u4eca\u56de\u3082Ruby on Rails\u30cd\u30bf\u3067\u3059\u3002 \u5927\u62b5\u306e\u696d\u52d9\u30b7\u30b9\u30c6\u30e0\u3067\u306f\u305f\u3044\u3066\u3044\u30c7\u30fc\u30bf\u3092\u691c\u7d22\u3059\u308b\u753b\u9762\u304c\u3042\u308a\u307e\u3059\u3002\u305d\u3093\u306a\u691c\u7d22\u753b\u9762\u3067\u3059\u304c\u3001\u7c21\u5358\u306a\u691c\u7d22\u6761\u4ef6\u3067\u3042\u308c\u3070\u3044\u3044\u306e\u3067\u3059\u304c\u3001 \u5927\u62b5\u306f\u8907\u6570\u306e\u30e2\u30c7\u30eb\u304c\u7d61\u307f\u3001\u30a2\u30bd\u30b7\u30a8\u30fc\u30b7\u30e7\u30f3\u5148 &hellip; <a href=\"https:\/\/www.prime-architect.co.jp\/myblog\/ruby-on-rails-1566\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;Ruby on Rails\u3067\u8907\u6570\u306e\u30e2\u30c7\u30eb\u304c\u7d61\u3080\u691c\u7d22\u753b\u9762\u306e\u4f5c\u308a\u65b9&#8221; \u306e<\/span>\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[21],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.prime-architect.co.jp\/myblog\/wp-json\/wp\/v2\/posts\/1566"}],"collection":[{"href":"https:\/\/www.prime-architect.co.jp\/myblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.prime-architect.co.jp\/myblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.prime-architect.co.jp\/myblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.prime-architect.co.jp\/myblog\/wp-json\/wp\/v2\/comments?post=1566"}],"version-history":[{"count":7,"href":"https:\/\/www.prime-architect.co.jp\/myblog\/wp-json\/wp\/v2\/posts\/1566\/revisions"}],"predecessor-version":[{"id":1573,"href":"https:\/\/www.prime-architect.co.jp\/myblog\/wp-json\/wp\/v2\/posts\/1566\/revisions\/1573"}],"wp:attachment":[{"href":"https:\/\/www.prime-architect.co.jp\/myblog\/wp-json\/wp\/v2\/media?parent=1566"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.prime-architect.co.jp\/myblog\/wp-json\/wp\/v2\/categories?post=1566"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.prime-architect.co.jp\/myblog\/wp-json\/wp\/v2\/tags?post=1566"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}