{"id":7920,"date":"2019-12-05T21:55:37","date_gmt":"2019-12-05T12:55:37","guid":{"rendered":"http:\/\/www.code-magagine.com\/?p=7920"},"modified":"2019-12-05T23:17:45","modified_gmt":"2019-12-05T14:17:45","slug":"%e3%80%90ruby-on-rails%e3%80%91%e3%80%8c%e6%a4%9c%e7%b4%a2%e6%a9%9f%e8%83%bd%e3%80%8d%e3%81%ae%e5%ae%9f%e8%a3%85ransack","status":"publish","type":"post","link":"http:\/\/www.code-magagine.com\/?p=7920","title":{"rendered":"\u3010Ruby on Rails\u3011\u300c\u691c\u7d22\u6a5f\u80fd\u300d\u306e\u5b9f\u88c5(Ransack)"},"content":{"rendered":"<h2>Ransack\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\u3002<\/h2>\n<p>Gemfile\u306b\u4e0b\u8a18\u306e\u8a18\u8ff0\u3092\u8ffd\u52a0\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">gem 'ransack'<\/pre>\n<p>bundle\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\u3002<\/p>\n<pre class=\"lang:default decode:true \">bundle<\/pre>\n<p>Rails\u3092\u518d\u8d77\u52d5\u3057\u307e\u3059\u3002<\/p>\n<h2>Ransack\u304c\u63d0\u4f9b\u3059\u308b\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u3067\u4f7f\u3048\u308b\u30e1\u30bd\u30c3\u30c9<\/h2>\n<h3>\u691c\u7d22\u3059\u308b\u30e1\u30bd\u30c3\u30c9<\/h3>\n<p>\u5bfe\u8c61\u306e\u30e2\u30c7\u30eb\u304b\u3089\u691c\u7d22\u753b\u9762\u3067\u6307\u5b9a\u3057\u305f\u30d1\u30e9\u30e1\u30fc\u30bf\u3067\u691c\u7d22\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">\u30e2\u30c7\u30eb\u30aa\u30d6\u30b8\u30a7\u30af\u30c8.ransack(params[:q])<\/pre>\n<h3>\u691c\u7d22\u7d50\u679c\u3092\u8fd4\u3059\u30e1\u30bd\u30c3\u30c9<\/h3>\n<p>\u4e0a\u8a18\u306e\u691c\u7d22\u3059\u308b\u30e1\u30bd\u30c3\u30c9\u306b\u3066\u53d6\u5f97\u3057\u305f\u7d50\u679c\u30aa\u30d6\u30b8\u30a7\u30af\u30c8(<span class=\"crayon-v\">Ransack<\/span><span class=\"crayon-o\">::<\/span><span class=\"crayon-i\">Search<\/span>\u30aa\u30d6\u30b8\u30a7\u30af\u30c8)\u306b\u5bfe\u3057\u3066result\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3046\u3053\u3068\u3067\u691c\u7d22\u5bfe\u8c61\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u7d5e\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">Ransack::Search\u30aa\u30d6\u30b8\u30a7\u30af\u30c8.result(\u30aa\u30d7\u30b7\u30e7\u30f3)<\/pre>\n<h4>:distinct<\/h4>\n<p>\u691c\u7d22\u7d50\u679c\u304c\u8907\u6570\u540c\u3058\u3082\u306e\u304c\u51fa\u3066\u304d\u305f\u5834\u5408\u306f\u91cd\u8907\u3092\u6392\u9664\u3057\u307e\u3059\u3002\uff08SQL\u306edistinct\u3068\u540c\u3058\uff09<\/p>\n<h2>Ransack\u304c\u63d0\u4f9b\u3059\u308b\u30d3\u30e5\u30fc\u3067\u4f7f\u3048\u308b\u30d8\u30eb\u30d1\u30fc<\/h2>\n<h3>search_form_for<\/h3>\n<p>\u691c\u7d22\u7528\u306e\u30d5\u30a9\u30fc\u30e0\u3092\u753b\u9762\u306b\u8ffd\u52a0\u3067\u304d\u307e\u3059\u3002<\/p>\n<h3>sort_link<\/h3>\n<p>\u8868\u306e\u30ab\u30e9\u30e0\u306b\u6607\u9806\u3001\u964d\u9806\u3068\u4ea4\u4e92\u306b\u5165\u308c\u66ff\u3048\u308b\u30ea\u30f3\u30af\u3092\u8ffd\u52a0\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">sort_link(Ransack::Search\u30aa\u30d6\u30b8\u30a7\u30af\u30c8,\u5bfe\u8c61\u30ab\u30e9\u30e0\uff09<\/pre>\n<h3>\u691c\u7d22\u30de\u30c3\u30c1\u30e3\u30fc<\/h3>\n<p>ransack\u304c\u7528\u610f\u3057\u3066\u3044\u308b\u691c\u7d22\u30de\u30c3\u30c1\u30e3\u30fc\u3067\u3059\u3002form\u306e\u5404\u5165\u529b\u30d5\u30a3\u30fc\u30eb\u30c9\u306ename\u5c5e\u6027\u306b\u6307\u5b9a\u3057\u307e\u3059\u3002\u5185\u90e8\u7684\u306b\u306fSQL\u6587\u3092\u4f5c\u6210\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<h4>(\u691c\u7d22\u5bfe\u8c61\u30ab\u30e9\u30e0)_cont<\/h4>\n<p>contains\u306e\u7565\u3067<span style=\"color: #ff0000;\"><strong>\u691c\u7d22\u6587\u5b57\u5217\u3092\u542b\u3080\u3082\u306e\u3092\u691c\u7d22<\/strong><\/span>\u3057\u307e\u3059\u3002<\/p>\n<h4>(\u691c\u7d22\u30ab\u30e9\u30e0)_gteq<\/h4>\n<p>greater than or equal\u306e\u7565\u3067\u691c\u7d22\u30d5\u30a9\u30fc\u30e0\u306b\u5165<span style=\"color: #ff0000;\"><strong>\u529b\u3057\u305f\u5024\u3068\u540c\u3058\u304b\u305d\u308c\u3088\u308a\u5927\u304d\u3044\u3082\u306e\u3092\u691c\u7d22<\/strong><\/span>\u3057\u307e\u3059\u3002<\/p>\n<h2>\u691c\u7d22\u3067\u304d\u308b\u30ab\u30e9\u30e0\u3092\u7d5e\u308b\u306b\u306f\uff1f<\/h2>\n<p>\u73fe\u72b6\u3001\u30d5\u30a3\u30fc\u30eb\u30c9\u306ename\u5c5e\u6027\u3067ransack\u304c\u691c\u7d22\u3067\u304d\u308b\u6a5f\u80fd\u3092\u7d5e\u3063\u3066\u3044\u307e\u3059\u3002\u305f\u3060\u3001\u30e6\u30fc\u30b6\u30fc\u304c\u610f\u56f3\u7684\u306bname\u5c5e\u6027\u306e\u5024\u3092\u66f8\u304d\u63db\u3048\u308b\u3053\u3068\u3067\u4ed6\u306e\u30ab\u30e9\u30e0\u306b\u3064\u3044\u3066\u3082\u691c\u7d22\u3059\u308b\u3053\u3068\u304c\u53ef\u80fd\u306b\u306a\u3063\u3066\u3057\u307e\u3044\u307e\u3059\u3002<\/p>\n<p>\u30e2\u30c7\u30eb(app\/models\/\u30e2\u30c7\u30eb\u540d.rb)\u306b\u4e0b\u8a18\u306e\u8a18\u8ff0\u3092\u8ffd\u52a0\u3057\u307e\u3059\u3002\u8a31\u53ef\u3059\u308b\u30ab\u30e9\u30e0\u306b\u6307\u5b9a\u3057\u305f\u30ab\u30e9\u30e0\u3067\u3057\u304bransack\u3067\u306f\u691c\u7d22\u304c\u3067\u304d\u306a\u304f\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">def self.ransackable_attributes(auth_object = nil)\r\n  %w[\u8a31\u53ef\u3059\u308b\u30ab\u30e9\u30e01 \u8a31\u53ef\u3059\u308b\u30ab\u30e9\u30e02]\r\nend<\/pre>\n<p>\u3082\u3061\u308d\u3093\u3001Strong Parameter\u3067\u3082\u5165\u3063\u3066\u304f\u308b\u5024\u3092\u5236\u5fa1\u3059\u308b\u3053\u3068\u306f\u53ef\u80fd\u3067\u3057\u3087\u3046\u304c\u3053\u306e\u3088\u3046\u306b<span style=\"color: #ff0000;\"><strong>ransack\u306e\u6a5f\u80fd\u3092\u4f7f\u3063\u3066\u5236\u9650\u3059\u308b\u306e\u304c\u4e00\u822c\u7684<\/strong><\/span>\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002(Strong Parameter\u306f\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u30fc\u306e\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u306b\u5165\u3063\u3066\u304f\u308b\u3082\u306e\u3067\u30bd\u30fc\u30b9\u304c\u7389\u77f3\u6df7\u4ea4\u306b\u306a\u308a\u3084\u3059\u3044\u305f\u3081\u3067\u3059\u3002\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"Ransack\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\u3002 Gemfile\u306b\u4e0b\u8a18\u306e\u8a18\u8ff0\u3092\u8ffd\u52a0\u3057\u307e\u3059\u3002 gem 'ransack' bundle\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\u3002 bundle Rails\u3092\u518d\u8d77\u52d5\u3057\u307e\u3059\u3002 Ransack\u304c\u63d0\u4f9b\u3059\u308b\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u3067\u4f7f [&hellip;]","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[36],"tags":[],"_links":{"self":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/7920"}],"collection":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7920"}],"version-history":[{"count":14,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/7920\/revisions"}],"predecessor-version":[{"id":7934,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/7920\/revisions\/7934"}],"wp:attachment":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7920"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7920"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7920"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}