{"id":9847,"date":"2020-03-22T21:28:45","date_gmt":"2020-03-22T12:28:45","guid":{"rendered":"http:\/\/www.code-magagine.com\/?p=9847"},"modified":"2023-05-27T22:48:18","modified_gmt":"2023-05-27T13:48:18","slug":"%e3%80%90ruby-on-rails%e3%80%91active-record%e3%81%ae%e7%b5%9e%e3%82%8a%e8%be%bc%e3%81%bf%e3%83%a1%e3%82%bd%e3%83%83%e3%83%89%e3%81%be%e3%81%a8%e3%82%81","status":"publish","type":"post","link":"http:\/\/www.code-magagine.com\/?p=9847","title":{"rendered":"\u3010Ruby on Rails\u3011Active Record\u306e\u7d5e\u308a\u8fbc\u307f\u30e1\u30bd\u30c3\u30c9\u307e\u3068\u3081"},"content":{"rendered":"<h2>\u30c7\u30fc\u30bf\u53d6\u5f97\u7cfb<\/h2>\n<h3>where<\/h3>\n<p>\u30c7\u30fc\u30bf\u3092\u7d5e\u308a\u8fbc\u307f\u307e\u3059\u3002<span style=\"color: #ff0000;\"><strong>\u300cActiveRecord_Relation\u300d<\/strong><\/span>\u3068\u3044\u3046\u30af\u30e9\u30b9\u304c\u8fd4\u3063\u3066\u304d\u307e\u3059\u3002\u691c\u7d22\u306b\u30d2\u30c3\u30c8\u3057\u305f\u30c7\u30fc\u30bf\u306f\u5168\u4ef6\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3067\u304d\u307e\u3059\u304c\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u4e0a\u3067\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3068\u3057\u3066\u81ea\u7531\u306b\u64cd\u4f5c\u3059\u308b\u3053\u3068\u306f\u3067\u304d\u306a\u3044\u3067\u3059\u3002<\/p>\n<h3>\u4f7f\u7528\u4f8b<\/h3>\n<pre class=\"lang:default decode:true\">User.where(name: \"\u540d\u524d1\")<\/pre>\n<h3>find<\/h3>\n<p>\u30e2\u30c7\u30eb\u306eid\u3092\u6307\u5b9a\u3057\u3066\u30ec\u30b3\u30fc\u30c9\u3092\u62bd\u51fa\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<span style=\"color: #ff0000;\"><strong>\u30e2\u30c7\u30eb\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u304c\u5165\u624b\u3067\u304d\u307e\u3059\u3002<\/strong><\/span><\/p>\n<pre class=\"lang:default decode:true\">User.all.find(2)<\/pre>\n<p>\u306a\u304a\u3001\u4e0a\u8ff0\u3057\u305f<span style=\"color: #ff0000;\"><strong>where\u3067\u7d5e\u308a\u8fbc\u3093\u3067\u304b\u3089find\u30e1\u30bd\u30c3\u30c9\u3067\u30e2\u30c7\u30eb\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u53d6\u5f97\u3059\u308b<\/strong><\/span>\u3068\u3044\u3046\u3053\u3068\u3082\u53ef\u80fd\u3067\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">User.where(name: \"\u540d\u524d1\").find(1)<\/pre>\n<h3>find_by<\/h3>\n<p>\u30e2\u30c7\u30eb\u306eid\u4ee5\u5916\u306e\u30ab\u30e9\u30e0\u3092\u6307\u5b9a\u3057\u3066\u30ec\u30b3\u30fc\u30c9\u3092\u62bd\u51fa\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<span style=\"color: #ff0000;\"><strong>\u305f\u3060\u3057\u3001\u6700\u521d\u306b\u30d2\u30c3\u30c8\u3057\u305f1\u4ef6\u306e\u30c7\u30fc\u30bf\u3057\u304b\u53d6\u5f97\u3067\u304d\u306a\u3044\u3067\u3059\u3002\u30e2\u30c7\u30eb\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u304c\u5165\u624b\u3067\u304d\u307e\u3059\u3002<\/strong><\/span><\/p>\n<pre class=\"lang:default decode:true\">User.all.find_by(name:\"\u540d\u524d2\")<\/pre>\n<h3>pluck<\/h3>\n<p>DB\u304b\u3089\u6307\u5b9a\u3057\u305f\u30ab\u30e9\u30e0\u306e\u30c7\u30fc\u30bf\u3092<span style=\"color: #ff0000;\"><strong>Array(\u914d\u5217)<\/strong><\/span>\u3067\u4e00\u89a7\u53d6\u5f97\u3057\u307e\u3059\u3002<\/p>\n<h4>\u4f7f\u7528\u4f8b<\/h4>\n<p>User\u30e2\u30c7\u30eb\u304b\u3089name\u30ab\u30e9\u30e0\u306e\u5185\u5bb9\u3092\u53d6\u5f97\u3059\u308b\u4f8b\u3067\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">User.pluck(:name)<\/pre>\n<p>where\u3067\u6761\u4ef6\u3092\u7d5e\u3063\u3066\u304b\u3089\u53d6\u5f97\u3059\u308b\u3053\u3068\u3082\u53ef\u80fd\u3067\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">User.where(family_id:1).pluck(:name)<\/pre>\n<h3>map<\/h3>\n<p>DB\u304b\u3089\u6307\u5b9a\u3057\u305f\u30ab\u30e9\u30e0\u306e\u30c7\u30fc\u30bf\u3092<span style=\"color: #ff0000;\"><strong>Array(\u914d\u5217)<\/strong><\/span>\u3067\u4e00\u89a7\u53d6\u5f97\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">User.map(&amp;:name)<\/pre>\n<h3>pluck\u3068map\u306e\u4f7f\u3044\u5206\u3051<\/h3>\n<p><span style=\"color: #ff0000;\"><strong>pluck\u306e\u65b9\u304c1\u5ea6\u306eSQL\u767a\u884c\u306b\u304a\u3044\u3066\u306f\u6307\u5b9a\u3057\u305f\u30ab\u30e9\u30e0\u306e\u307f\u53d6\u5f97\u3059\u308b\u306e\u3067\u9ad8\u901f<\/strong><\/span>\u3067\u3059\u3002\u305f\u3060\u3001<span style=\"color: #ff0000;\"><strong>\u30eb\u30fc\u30d7\u51e6\u7406\u306e\u4e2d\u3067\u4f55\u5ea6\u3082\u30e1\u30bd\u30c3\u30c9\u3092\u5b9f\u884c\u3059\u308b\u5834\u5408\u306fmap\u306f\u4e00\u5ea6\u3057\u304bSQL\u3092\u767a\u884c\u3057\u306a\u3044\u306e\u3067\u9ad8\u901f<\/strong><\/span>\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<h2>order<\/h2>\n<p>\u30c7\u30fc\u30bf\u3092\u4e26\u3073\u66ff\u3048\u308b\u3002<\/p>\n<h3>\u6607\u9806\uff08\u30c7\u30d5\u30a9\u30eb\u30c8\uff09<\/h3>\n<pre class=\"lang:default decode:true\">User.all.order(:name)<\/pre>\n<h3>\u964d\u9806<\/h3>\n<pre class=\"lang:default decode:true \">User.all.order(name: \"DESC\")<\/pre>\n<h2>joins<\/h2>\n<p>\u4ed6\u306e\u95a2\u9023\u30c6\u30fc\u30d6\u30eb\u3068\u5185\u90e8\u7d50\u5408\u3092\u3057\u307e\u3059\u3002<\/p>\n<h3>\u4f7f\u7528\u4f8b<\/h3>\n<pre class=\"lang:default decode:true \">Family.joins(:user)<\/pre>\n<h2>merge<\/h2>\n<p>join\u306e\u7d50\u5408\u5148\u306escope\u3092\u4f7f\u3046\u5834\u5408\u306b\u4f7f\u7528\u3059\u308b\u3002merge\u3092\u5229\u7528\u3059\u308c\u3070\u7d50\u5408\u5148\u306escope\u3092\u547c\u3073\u51fa\u3059\u3053\u3068\u304c\u53ef\u80fd\u3067\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">class \u5b50\u30e2\u30c7\u30eb &lt; ApplicationRecord\r\n  scope :scope\u30e1\u30bd\u30c3\u30c9\u540d, -&gt; { where(xxx) }\r\nend\r\n\r\nclass \u89aa\u30e2\u30c7\u30eb &lt; ApplicationRecord\r\n  has_many :movies\r\nend\r\n\r\n\u89aa\u30e2\u30c7\u30eb.joins(:\u5b50\u30e2\u30c7\u30eb).merge(\u5b50\u30e2\u30c7\u30eb.scope\u30e1\u30bd\u30c3\u30c9\u540d)<\/pre>\n<h2>group<\/h2>\n<p>\u30b0\u30eb\u30fc\u30d7\u5316\u3059\u308b\u3002<\/p>\n<h3>\u4f7f\u7528\u4f8b<\/h3>\n<p>count\u95a2\u6570\u3068\u7d44\u307f\u5408\u308f\u305b\u305f\u4f8b<\/p>\n<pre class=\"lang:default decode:true\">User.all.group(:name).count<\/pre>\n<h4>\u51fa\u529b\u7d50\u679c<\/h4>\n<p>\u4e0b\u8a18\u306e\u3088\u3046\u306b\u7a2e\u985e\u3054\u3068\u306b\u4ef6\u6570\u304c\u96c6\u8a08\u3057\u3066\u30cf\u30c3\u30b7\u30e5\u5f62\u5f0f\u3067\u51fa\u529b\u3055\u308c\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">{\"\u540d\u524d1\"=&gt;1, \"\u540d\u524d2\"=&gt;1, \"\u540d\u524d3\"=&gt;1, \"\u540d\u524d4\"=&gt;1, \"\u540d\u524d5\"=&gt;1, \"\u540d\u524d6\"=&gt;1, \"\u540d\u524d7\"=&gt;1, \"\u540d\u524d8\"=&gt;1}<\/pre>\n<h2>select<\/h2>\n<p>SQL\u306eSELECT\u53e5\u3067\u3059\u3002\u30ab\u30e9\u30e0\u3092\u7d5e\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002ActiveRecord\u304c\u8fd4\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">User.select('name')<\/pre>\n<h2>limit<\/h2>\n<p>SQL\u306eLIMIT\u53e5\u3067\u3059\u3002\u53d6\u5f97\u500b\u6570\u3092\u6307\u5b9a\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">User.all.limit(2)<\/pre>\n<h2>select\u3068pluck\u306e\u6bd4\u8f03<\/h2>\n<h3>SQL\u306f\u540c\u3058<\/h3>\n<p>\u751f\u6210\u3055\u308c\u308bSQL\u306fselect\u3082pluck\u3082\u3069\u3061\u3089\u3082\u540c\u3058\u3067\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">SELECT `\u30e2\u30c7\u30eb\u540d`.`\u30ab\u30e9\u30e0\u540d` FROM `\u30e2\u30c7\u30eb\u540d`<\/pre>\n<h3>\u30b5\u30d6\u30af\u30a8\u30ea\u3067\u4f7f\u3046\u5834\u5408<\/h3>\n<p>\u30b5\u30d6\u30af\u30a8\u30ea\u5185\u3067\u305d\u308c\u305e\u308c\u5b9f\u884c\u3057\u305f\u3089\u3069\u3061\u3089\u3082SQL\u306e\u767a\u884c\u56de\u6570\u306f1\u56de\u3067\u3042\u308a\u305d\u3053\u307e\u3067\u5927\u304d\u306a\u9055\u3044\u306f\u306a\u304b\u3063\u305f\u3002<\/p>\n<h3>select\u306e\u5834\u5408<\/h3>\n<pre class=\"lang:default decode:true\">Project.where(id:Category.find(1).category_projects.select(:project_id))\r\n\r\nSELECT \"projects\".* FROM \"projects\" WHERE \"projects\".\"id\" IN (SELECT \"category_projects\".\"project_id\" FROM \"category_projects\" WHERE \"category_projects\".\"category_id\" = ?) LIMIT ?<\/pre>\n<h4>pluck\u306e\u5834\u5408<\/h4>\n<pre class=\"lang:default decode:true  \">Project.where(id:Category.find(1).category_projects.pluck(:project_id))\r\n\r\nSELECT \"projects\".* FROM \"projects\" WHERE \"projects\".\"id\" IN (?, ?) LIMIT ?<\/pre>\n","protected":false},"excerpt":{"rendered":"\u30c7\u30fc\u30bf\u53d6\u5f97\u7cfb where \u30c7\u30fc\u30bf\u3092\u7d5e\u308a\u8fbc\u307f\u307e\u3059\u3002\u300cActiveRecord_Relation\u300d\u3068\u3044\u3046\u30af\u30e9\u30b9\u304c\u8fd4\u3063\u3066\u304d\u307e\u3059\u3002\u691c\u7d22\u306b\u30d2\u30c3\u30c8\u3057\u305f\u30c7\u30fc\u30bf\u306f\u5168\u4ef6\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3067\u304d\u307e\u3059\u304c\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u4e0a\u3067\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3068\u3057\u3066\u81ea\u7531\u306b\u64cd\u4f5c\u3059\u308b [&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\/9847"}],"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=9847"}],"version-history":[{"count":14,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/9847\/revisions"}],"predecessor-version":[{"id":20514,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/9847\/revisions\/20514"}],"wp:attachment":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9847"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9847"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9847"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}