{"id":3451,"date":"2014-03-25T11:24:58","date_gmt":"2014-03-25T11:24:58","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/25\/rails-3-scope-only-select-certain-attributes-for-a-has_many-relationship-collection-of-common-programming-errors\/"},"modified":"2014-03-25T11:24:58","modified_gmt":"2014-03-25T11:24:58","slug":"rails-3-scope-only-select-certain-attributes-for-a-has_many-relationship-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/25\/rails-3-scope-only-select-certain-attributes-for-a-has_many-relationship-collection-of-common-programming-errors\/","title":{"rendered":"Rails 3 scope only select certain attributes for a has_many relationship-Collection of common programming errors"},"content":{"rendered":"<p>This looks like it should be something pretty easy but I can&#8217;t seem to get it to work. I have a model with a has_many relationship and I&#8217;d like a scope on the parent that allows me to select only certain attributes for each.<\/p>\n<p>An example:<\/p>\n<pre><code>class Bakery &lt; ActiveRecord::Base\n  has_many :pastries\n  scope :summary, select([:id, :name, 'some option calling pastries.summary'])\n\nclass Pastry &lt; ActiveRecord::Base\n  belongs_to :bakery\n  scope :summary, select([:id, :image_url])\n<\/code><\/pre>\n<p>I&#8217;d like to be able to call something like <code>Bakery.first.summary<\/code> and get a Bakery model with only the id and name populated and for each pastry in it&#8217;s pastries array to only have the id and image_url attributes populated.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This looks like it should be something pretty easy but I can&#8217;t seem to get it to work. I have a model with a has_many relationship and I&#8217;d like a scope on the parent that allows me to select only certain attributes for each. An example: class Bakery &lt; ActiveRecord::Base has_many :pastries scope :summary, select([:id, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3451","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3451","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=3451"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3451\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3451"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3451"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3451"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}