{"id":12433,"date":"2020-12-19T19:11:11","date_gmt":"2020-12-19T10:11:11","guid":{"rendered":"http:\/\/www.code-magagine.com\/?p=12433"},"modified":"2023-05-27T22:42:42","modified_gmt":"2023-05-27T13:42:42","slug":"%e3%80%90ruby-on-rails%e3%80%91%e3%83%a2%e3%83%87%e3%83%ab%e5%86%85%e3%81%a7scope%e3%82%92%e4%bd%bf%e3%81%86%e3%81%8b%e3%80%81%e3%83%a1%e3%82%bd%e3%83%83%e3%83%89%e3%82%92%e4%bd%bf%e3%81%86%e3%81%8b","status":"publish","type":"post","link":"http:\/\/www.code-magagine.com\/?p=12433","title":{"rendered":"\u3010Ruby on Rails\u3011\u30e2\u30c7\u30eb\u306e\u57fa\u672c\u3001\u30c7\u30fc\u30bf\u578b\u3001scope\u3001\u30e1\u30bd\u30c3\u30c9\uff08\u30af\u30e9\u30b9\u3001\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\uff09\u306e\u9055\u3044\u3001\u59d4\u8b72"},"content":{"rendered":"<h2>Rails\u304c\u30e2\u30c7\u30eb\u306b\u81ea\u52d5\u7684\u306b\u7528\u610f\u3057\u3066\u304f\u308c\u308b\u9805\u76ee<\/h2>\n<p>\u4e0b\u8a18\u306e3\u9805\u76ee\u306f\u4e00\u822c\u7684\u306b\u30e2\u30c7\u30eb\u3067\u3088\u304f\u5229\u7528\u3055\u308c\u308b\u5c5e\u6027\u306a\u306e\u3067Rails\u304c\u81ea\u52d5\u7684\u306b\u7528\u610f\u3057\u3066\u304f\u308c\u307e\u3059\u3002<\/p>\n<ul>\n<li>ID<\/li>\n<li>\u767b\u9332\u65e5\u6642<\/li>\n<li>\u66f4\u65b0\u65e5\u6642<\/li>\n<\/ul>\n<h2>\u30e2\u30c7\u30eb\u306e\u30c7\u30fc\u30bf\u578b\u306b\u3064\u3044\u3066<\/h2>\n<p>Rails\u306e\u30e2\u30c7\u30eb\u306b\u306f\u30c7\u30fc\u30bf\u578b\u304c\u3042\u308a\u307e\u3059\u3002<span style=\"color: #ff0000;\"><strong>RDBMS\u304c\u7528\u610f\u3057\u3066\u3044\u308b\u30c7\u30fc\u30bf\u578b\u3067\u306f\u306a\u304fRails\u304cRDBMS\u3054\u3068\u306e\u5dee\u7570\u3092\u5438\u53ce\u3057\u305f\u62bd\u8c61\u7684\u306a\u578b\u3068\u3057\u3066\u7528\u610f\u3057\u3066\u3044\u308b\u6982\u5ff5\u306e\u3053\u3068<\/strong><\/span>\u3067\u3059\u3002Rails\u306e\u5185\u90e8\u3067RDBMS\u306e\u30c7\u30fc\u30bf\u578b\u306b\u30de\u30c3\u30d4\u30f3\u30b0\u3055\u308c\u3066\u3044\u307e\u3059\u3002\uff08\u4e0b\u8a18\u306fDB\u88fd\u54c1\u306b\u4f9d\u5b58\u3057\u306a\u3044\u66f8\u304d\u65b9\u306b\u306a\u308b\u3002\uff09<\/p>\n<h3>boolean<\/h3>\n<p>\u771f\u507d\u5024<\/p>\n<h3>integer<\/h3>\n<p>\u7b26\u5408\u4ed8\u304d\u6574\u6570<\/p>\n<h3>float<\/h3>\n<p>\u6d6e\u52d5\u5c0f\u6570\u70b9\u6570<\/p>\n<h3>string<\/h3>\n<p>\u77ed\u3044\u6587\u5b57\u5217\u3092\u6271\u3046\u3002<\/p>\n<h3>text<\/h3>\n<p>\u9577\u3044\u6587\u5b57\u5217\u3092\u6271\u3046\u3002<\/p>\n<h3>date<\/h3>\n<p>\u65e5\u4ed8<\/p>\n<h3>datetime<\/h3>\n<p>\u65e5\u6642<\/p>\n<h2>scope\uff08\u540d\u524d\u4ed8\u304d\u30b9\u30b3\u30fc\u30d7\uff09<\/h2>\n<p>\u4e0a\u8a18\u306e\u7d5e\u308a\u8fbc\u307f\u6761\u4ef6\u306e\u30e1\u30bd\u30c3\u30c9\u306f\u30ab\u30b9\u30bf\u30e0\u7528\u306e\u30af\u30a8\u30ea\u30fc\u3068\u3057\u3066scope\u3068\u3044\u3046\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3048\u3070\u30bd\u30fc\u30b9\u3092\u8aad\u307f\u3084\u3059\u304f\u3067\u304d\u307e\u3059\u3002\u5225\u540d\u3067<span style=\"color: #ff0000;\"><strong>\u300c\u540d\u524d\u4ed8\u304d\u30b9\u30b3\u30fc\u30d7\u300d<\/strong><\/span>\u3068\u3082\u547c\u3073\u307e\u3059\u3002<\/p>\n<h3>\u7528\u9014<\/h3>\n<p>\u518d\u5229\u7528\u3067\u304d\u305d\u3046\u306a\u30d1\u30fc\u30c4\u3092scope\u3068\u3057\u3066\u5b9a\u7fa9\u3059\u308b\u305f\u3081\u306b\u5229\u7528\u3057\u307e\u3059\u3002\u306a\u304a\u3001\u3061\u3087\u3063\u3068\u3057\u305fwhere\u306e\u3088\u3046\u306b\u7c21\u5358\u306a\u30d1\u30fc\u30c4\u3067\u3042\u3063\u3066\u3082\u5834\u5408\u306b\u3088\u3063\u3066\u306f\u5916\u51fa\u3057\u3059\u308b\u305f\u3081\u306b\u5229\u7528\u3057\u305f\u308a\u3057\u307e\u3059\u3002\u305f\u3060\u3001\u4f8b\u3048\u3070\u300corder(:\u30ab\u30e9\u30e0\u540d)\u300d\u306e\u3088\u3046\u306b\u5358\u7d14\u3059\u304e\u308b\u5185\u5bb9\u306e\u3082\u306e\u306b\u95a2\u3057\u3066\u306f\u4e0d\u8981\u3067\u3057\u3087\u3046\u3002\uff08order(:\u30ab\u30e9\u30e0 desc)\u3068\u304b\u3067\u6700\u65b0\u306e\u3082\u306e\u3092\u53d6\u5f97\u3059\u308b\u30ec\u30d9\u30eb\u306a\u3089\u5fc5\u8981\u304b\u3082\u3002\uff09<\/p>\n<h3>\u5b9f\u88c5\u4f8b<\/h3>\n<p>\u4e0b\u8a18\u306e\u8a18\u8ff0\u3092\u30e2\u30c7\u30eb\u30d5\u30a1\u30a4\u30eb(models\/\u30e2\u30c7\u30eb\u30aa\u30d6\u30b8\u30a7\u30af\u30c8.rb)\u306b\u8a18\u8ff0\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">scope :\u30ab\u30b9\u30bf\u30e0\u30af\u30a8\u30ea\u30fc\u540d, -&gt; {\u6761\u4ef6}<\/pre>\n<p>\u4f8b\u3048\u3070\u3001User\u3068\u3044\u3046\u30e2\u30c7\u30eb\u306b\u4e0b\u8a18\u306e\u30ab\u30b9\u30bf\u30af\u30a8\u30ea\u30fc\u3092\u5b9a\u7fa9\u3057\u305f\u3068\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">scope :whereAdmin, -&gt; { where{name:'admin'}}<\/pre>\n<p>\u30bd\u30fc\u30b9\u306e\u65b9\u3067\u306f\u4e0b\u8a18\u306e\u3088\u3046\u306b\u4f7f\u3048\u305f\u308a\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">user.whereAdmin.first<\/pre>\n<h3>\u547d\u540d<\/h3>\n<p>scope\u306e\u547d\u540d\u306f<span style=\"color: #ff0000;\"><strong>\u300cin_xxx\u300d<\/strong><\/span>\u3068\u3059\u308b\u3053\u3068\u304c\u591a\u3044\u3088\u3046\u3067\u3059\u3002<\/p>\n<h2>\u30af\u30e9\u30b9\u30e1\u30bd\u30c3\u30c9<\/h2>\n<p>\u30af\u30e9\u30b9\u5168\u4f53\u3067\u4f7f\u3048\u308b\u30e1\u30bd\u30c3\u30c9\u3067\u3059\u3002\u30e2\u30c7\u30eb\u30af\u30e9\u30b9\u5168\u4f53\u3092\u6271\u3044\u307e\u3059\u3002\uff08\u4f8b\u3048\u3070\u3001\u30e6\u30fc\u30b6\u30fc\u306e\u4e00\u89a7\u60c5\u5831\uff09<\/p>\n<p>\u30e2\u30c7\u30eb\u5185\u306b\u30af\u30e9\u30b9\u30e1\u30bd\u30c3\u30c9 \u3092\u5b9a\u7fa9\u3057\u305f\u3089scope\u3068\u306f\u6a5f\u80fd\u7684\u306b\u5168\u304f\u540c\u3058\u306b\u306a\u308a\u307e\u3059\u3002\u305f\u3060\u3001<span style=\"color: #ff0000;\"><strong>scope\u3088\u308a\u3082\u884c\u6570\u304c\u9577\u304f\u306a\u3063\u3066\u3057\u307e\u3046\u306e\u3067\u4e00\u822c\u7684\u306b\u306f\u30af\u30e9\u30b9\u30e1\u30bd\u30c3\u30c9 \u3088\u308a\u306fscope\u304c\u597d\u307e\u308c\u307e\u3059\u3002<\/strong><\/span><\/p>\n<pre class=\"lang:default decode:true\">class \u30e2\u30c7\u30eb\u30af\u30e9\u30b9\u540d\r\n  def self.\u30e1\u30bd\u30c3\u30c9\u540d\r\n    where(\u30ab\u30e9\u30e0\u540d: \u5024)\r\n  end\r\nend<\/pre>\n<h2>\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u30e1\u30bd\u30c3\u30c9<\/h2>\n<p>\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3067\u4f7f\u3048\u308b\u30e1\u30bd\u30c3\u30c9\u3067\u3059\u3002\u7279\u5b9a\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u6271\u3044\u307e\u3059\u3002\uff08\u4f8b\u3048\u3070\u3001\u7279\u5b9a\u306e\u30e6\u30fc\u30b6\u30fc\u7b49\uff09\u3001\u4f8b\u3048\u3070\u3001\u4e0b\u8a18\u306e\u3088\u3046\u306b<span style=\"color: #ff0000;\"><strong>self.\u30ab\u30e9\u30e0\u540d<\/strong><strong>\u3068\u6307\u5b9a\u3059\u308b\u3053\u3068\u3067\u7279\u5b9a\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u306e\u30ab\u30e9\u30e0\u540d\u3092\u8fd4\u3059\u30e1\u30bd\u30c3\u30c9\u306b\u306a\u308a\u307e\u3059\u3002<\/strong><\/span><\/p>\n<pre class=\"lang:default decode:true \">class \u30e2\u30c7\u30eb\u30af\u30e9\u30b9\r\n    def \u30e1\u30bd\u30c3\u30c9\u540d\r\n        self.\u30ab\u30e9\u30e0\u540d\r\n    end\r\nend<\/pre>\n<h2>\u59d4\u8b72\u3068\u306f\uff1f<\/h2>\n<p>\u30bd\u30fc\u30b9\u306e\u8a18\u8ff0\u91cf\u3092\u6e1b\u3089\u3059\u3053\u3068\u304c\u3067\u304d\u308b\u6a5f\u80fd\u3067\u3059\u3002<\/p>\n<h3>\u4f7f\u3044\u65b9<\/h3>\n<p>class\u5185\u306b\u4e0b\u8a18\u306e\u3088\u3046\u306b\u5b9a\u7fa9\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">delegate :\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u30e1\u30bd\u30c3\u30c9, to: :\u30e1\u30bd\u30c3\u30c9\u540d\uff08\u3053\u306e\u30e1\u30bd\u30c3\u30c9\u304c\u8fd4\u3059\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u30e1\u30bd\u30c3\u30c9\u3092\u59d4\u8b72\u3059\u308b\u3002\uff09<\/pre>\n<h3>\u5177\u4f53\u4f8b<\/h3>\n<pre class=\"lang:default decode:true\">delegate :raw, to: :view_context<\/pre>\n<p>raw\u3068\u3044\u3046\u30e1\u30bd\u30c3\u30c9\u3092view_context\u30e1\u30bd\u30c3\u30c9\u304c\u8fd4\u3059\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u59d4\u8b72\u3057\u307e\u3059\u3002\u3053\u306e\u5834\u5408\u3001view_context\u306f\u5168\u3066\u306eview\u30d8\u30eb\u30d1\u30fc\u3092\u6240\u6709\u3057\u3066\u3044\u308b\u306e\u3067\u5f53\u7136\u306e\u3054\u3068\u304fraw\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3046\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<h3>\u59d4\u8b72\u5b9a\u7fa9\u524d<\/h3>\n<pre class=\"lang:default decode:true\">view_context.raw('\u3042\u3044\u3046')\r\nview_context.raw('\u3048\u304a')\r\n<\/pre>\n<h3>\u59d4\u8b72\u5b9a\u7fa9\u5f8c<\/h3>\n<pre class=\"lang:default decode:true \">raw('\u3042\u3044\u3046')\r\nraw('\u3048\u304a')<\/pre>\n","protected":false},"excerpt":{"rendered":"Rails\u304c\u30e2\u30c7\u30eb\u306b\u81ea\u52d5\u7684\u306b\u7528\u610f\u3057\u3066\u304f\u308c\u308b\u9805\u76ee \u4e0b\u8a18\u306e3\u9805\u76ee\u306f\u4e00\u822c\u7684\u306b\u30e2\u30c7\u30eb\u3067\u3088\u304f\u5229\u7528\u3055\u308c\u308b\u5c5e\u6027\u306a\u306e\u3067Rails\u304c\u81ea\u52d5\u7684\u306b\u7528\u610f\u3057\u3066\u304f\u308c\u307e\u3059\u3002 ID \u767b\u9332\u65e5\u6642 \u66f4\u65b0\u65e5\u6642 \u30e2\u30c7\u30eb\u306e\u30c7\u30fc\u30bf\u578b\u306b\u3064\u3044\u3066 Rails\u306e\u30e2\u30c7\u30eb\u306b\u306f\u30c7\u30fc [&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\/12433"}],"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=12433"}],"version-history":[{"count":13,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/12433\/revisions"}],"predecessor-version":[{"id":20494,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/12433\/revisions\/20494"}],"wp:attachment":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=12433"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=12433"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=12433"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}