{"id":7695,"date":"2019-12-01T00:30:23","date_gmt":"2019-11-30T15:30:23","guid":{"rendered":"http:\/\/www.code-magagine.com\/?p=7695"},"modified":"2023-05-27T22:57:30","modified_gmt":"2023-05-27T13:57:30","slug":"%e3%80%90ruby-on-rails%e3%80%91%e3%83%a2%e3%83%87%e3%83%ab%e3%81%ae%e3%83%87%e3%83%bc%e3%82%bf%e3%82%92%e7%b5%9e%e3%82%8a%e8%be%bc%e3%82%80%e4%be%bf%e5%88%a9%e6%a9%9f%e8%83%bd","status":"publish","type":"post","link":"http:\/\/www.code-magagine.com\/?p=7695","title":{"rendered":"\u3010Ruby on Rails\u3011\u30e2\u30c7\u30eb\u306e\u30c7\u30fc\u30bf\u3092\u7d5e\u308a\u8fbc\u3080\u4fbf\u5229\u6a5f\u80fd(scope\u3082\uff09"},"content":{"rendered":"<p>Rails\u306b\u306f\u30e2\u30c7\u30eb\u304b\u3089\u30c7\u30fc\u30bf\u3092\u53d6\u308a\u51fa\u3057\u3055\u3089\u306b\u305d\u306e\u4e2d\u304b\u3089\u30c7\u30fc\u30bf\u3092\u62bd\u51fa\u3059\u308b\u305f\u3081\u306e\u4fbf\u5229\u306a\u30e1\u30bd\u30c3\u30c9\u304c\u3044\u304f\u3064\u304b\u6a19\u6e96\u3067\u7528\u610f\u3055\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n<h2>\u96db\u5f62<\/h2>\n<pre class=\"lang:default decode:true\">\u30e2\u30c7\u30eb\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u7b49.\u7d5e\u308a\u8fbc\u307f\u6761\u4ef6.\u5b9f\u884c\u90e8\u5206<\/pre>\n<h3>\u4f8b<\/h3>\n<p>admin\u3068\u3044\u3046\u540d\u524d\u306e\u30e6\u30fc\u30b6\u30fc\u3067\u7d5e\u308a\u8fbc\u307f\u6700\u521d\u306e\u30ec\u30b3\u30fc\u30c9\u3092\u53d6\u5f97\u3059\u308b\u3002<\/p>\n<pre class=\"lang:default decode:true\">User.where(name:'admin').first<\/pre>\n<h2>\u30e2\u30c7\u30eb\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u7b49<\/h2>\n<p>\u3082\u3061\u308d\u3093\u30e2\u30c7\u30eb\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u305d\u306e\u307e\u307e\u6307\u5b9a\u3059\u308b\u3053\u3068\u3082\u53ef\u80fd\u3067\u3059\u3057\u300c\u89aa\u30aa\u30d6\u30b8\u30a7\u30af\u30c8.\u5b50\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u300d\u306e\u3088\u3046\u306bhas_many\u7b49\u3067\u95a2\u9023\u3065\u3051\u305f\u30c6\u30fc\u30d6\u30eb\u3092\u6307\u5b9a\u3059\u308b\u3053\u3068\u3082\u53ef\u80fd\u3067\u3059\u3002<\/p>\n<h2>\u7d5e\u308a\u8fbc\u307f\u6761\u4ef6<\/h2>\n<h3>where\u30e1\u30bd\u30c3\u30c9<\/h3>\n<p>SQL\u306ewhere\u53e5\u3067\u3059\u3002\u91cd\u306d\u304c\u3051\u53ef\u80fd\u3067\u305d\u306e\u5834\u5408\u306fAND\u6761\u4ef6\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<h3>order\u30e1\u30bd\u30c3\u30c9<\/h3>\n<p>SQL\u306eorder by\u53e5\u3067\u3059\u3002<\/p>\n<h3>joins\u30e1\u30bd\u30c3\u30c9<\/h3>\n<p>SQL\u306ejoin\u53e5\u3067\u3059\u3002<\/p>\n<h3>group\u30e1\u30bd\u30c3\u30c9<\/h3>\n<p>SQL\u306egroup by\u53e5\u3067\u3059\u3002<\/p>\n<h3>select\u30e1\u30bd\u30c3\u30c9<\/h3>\n<p>SQL\u306eselect\u53e5\u3067\u3059\u3002\u6307\u5b9a\u3057\u306a\u3044\u5834\u5408\u306f\u5168\u30ec\u30b3\u30fc\u30c9\u3092\u53d6\u5f97\u3059\u308b\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<h3>limit\u30e1\u30bd\u30c3\u30c9<\/h3>\n<p>SQL\u306elimit\u53e5\u3067\u3059\u3002<\/p>\n<h3>distinct\u30e1\u30bd\u30c3\u30c9<\/h3>\n<p>SQL\u306edistinct\u3067\u3059\u3002\u91cd\u8907\u3092\u6392\u9664\u3057\u307e\u3059\u3002<\/p>\n<h3>all\u30e1\u30bd\u30c3\u30c9<\/h3>\n<p>\u5b9f\u8cea\u4f55\u3082\u3057\u307e\u305b\u3093\u3002\u305f\u3060\u5168\u4ef6\u53d6\u5f97\u3059\u308b\u3053\u3068\u3092\u660e\u793a\u3057\u305f\u3044\u5834\u5408\u306b\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<h3>none\u30e1\u30bd\u30c3\u30c9<\/h3>\n<p>\u4f55\u3082\u30d2\u30c3\u30c8\u3057\u306a\u3044\u691c\u7d22\u6761\u4ef6\u3067\u3059\u3002<\/p>\n<h2>\u5b9f\u884c\u90e8\u5206<\/h2>\n<h3>find\u30e1\u30bd\u30c3\u30c9<\/h3>\n<p>ID\u3092\u6307\u5b9a\u3057\u3066\u30ec\u30b3\u30fc\u30c9\u3092\u53d6\u5f97\u3059\u308b\u3002<\/p>\n<h3>find_by\u30e1\u30bd\u30c3\u30c9<\/h3>\n<p>\u6761\u4ef6\u3092\u6307\u5b9a\u3057\u3066\u898b\u3064\u304b\u3063\u305f\u30ec\u30b3\u30fc\u30c9\u3092\u8fd4\u3059\u3002\u898b\u3064\u304b\u3089\u306a\u304b\u3063\u305f\u5834\u5408\u306fnil\u3092\u8fd4\u3057\u307e\u3059\u3002<\/p>\n<h3>first\u30e1\u30bd\u30c3\u30c9<\/h3>\n<p>\u6700\u521d\u306e\u30ec\u30b3\u30fc\u30c9\u3092\u8fd4\u3057\u307e\u3059\u3002<\/p>\n<h3>last\u30e1\u30bd\u30c3\u30c9<\/h3>\n<p>\u6700\u5f8c\u306e\u30ec\u30b3\u30fc\u30c9\u3092\u8fd4\u3057\u307e\u3059\u3002<\/p>\n<h3>exists?\u30e1\u30bd\u30c3\u30c9<\/h3>\n<p>\u30ec\u30b3\u30fc\u30c9\u306e\u6709\u7121\u3092\u8fd4\u3057\u307e\u3059\u3002<\/p>\n<h3>count\u30e1\u30bd\u30c3\u30c9<\/h3>\n<p>\u30ec\u30b3\u30fc\u30c9\u4ef6\u6570\u3092\u8fd4\u3057\u307e\u3059\u3002<\/p>\n<h3>average\u30e1\u30bd\u30c3\u30c9<\/h3>\n<p>\u5e73\u5747\u3092\u53d6\u5f97\u3057\u307e\u3059\u3002SQL\u306eAVG\u95a2\u6570\u304c\u4f7f\u308f\u308c\u307e\u3059\u3002<\/p>\n<h3>maximum\u30e1\u30bd\u30c3\u30c9<\/h3>\n<p>\u6700\u5927\u5024\u3092\u53d6\u5f97\u3057\u307e\u3059\u3002SQL\u306eMAX\u95a2\u6570\u304c\u4f7f\u308f\u308c\u307e\u3059\u3002<\/p>\n<h3>minimum\u30e1\u30bd\u30c3\u30c9<\/h3>\n<p>\u6700\u5c0f\u5024\u3092\u53d6\u5f97\u3057\u307e\u3059\u3002SQL\u306eMIN\u95a2\u6570\u304c\u4f7f\u308f\u308c\u307e\u3059\u3002<\/p>\n<h3>update_all\u30e1\u30bd\u30c3\u30c9<\/h3>\n<p>\u5168\u30ec\u30b3\u30fc\u30c9\u3092\u5168\u3066\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u5316\u305b\u305a\u306b\u66f4\u65b0\u3059\u308b\u3002\u691c\u8a3c\u3084\u66f4\u65b0\u7cfb\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u306f\u547c\u3070\u308c\u307e\u305b\u3093\u3002<\/p>\n<h3>delete_all\u30e1\u30bd\u30c3\u30c9<\/h3>\n<p>\u5168\u30ec\u30b3\u30fc\u30c9\u3092\u5168\u3066\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u5316\u305b\u305a\u306b\u524a\u9664\u3059\u308b\u3002\u524a\u9664\u7cfb\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u306f\u547c\u3070\u308c\u307e\u305b\u3093\u3002<\/p>\n<h3>destroy_all\u30e1\u30bd\u30c3\u30c9<\/h3>\n<p>\u5168\u30ec\u30b3\u30fc\u30c9\u3092\u5168\u3066\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u5316\u3057\u3066\u524a\u9664\u3059\u308b\u3002\u524a\u9664\u7cfb\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u306f\u547c\u3070\u308c\u307e\u3059\u3002<\/p>\n<h3>\u6307\u5b9a\u306a\u3057<\/h3>\n<p>\u691c\u7d22\u7d50\u679c\u306e\u5168\u30ec\u30b3\u30fc\u30c9\u3092\u53d6\u5f97\u3057\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Rails\u306b\u306f\u30e2\u30c7\u30eb\u304b\u3089\u30c7\u30fc\u30bf\u3092\u53d6\u308a\u51fa\u3057\u3055\u3089\u306b\u305d\u306e\u4e2d\u304b\u3089\u30c7\u30fc\u30bf\u3092\u62bd\u51fa\u3059\u308b\u305f\u3081\u306e\u4fbf\u5229\u306a\u30e1\u30bd\u30c3\u30c9\u304c\u3044\u304f\u3064\u304b\u6a19\u6e96\u3067\u7528\u610f\u3055\u308c\u3066\u3044\u307e\u3059\u3002 \u96db\u5f62 \u30e2\u30c7\u30eb\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u7b49.\u7d5e\u308a\u8fbc\u307f\u6761\u4ef6.\u5b9f\u884c\u90e8\u5206 \u4f8b admin\u3068\u3044\u3046\u540d\u524d\u306e\u30e6\u30fc\u30b6\u30fc\u3067\u7d5e\u308a [&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\/7695"}],"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=7695"}],"version-history":[{"count":8,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/7695\/revisions"}],"predecessor-version":[{"id":20536,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/7695\/revisions\/20536"}],"wp:attachment":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7695"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7695"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7695"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}