{"id":521,"date":"2022-08-30T15:02:44","date_gmt":"2022-08-30T15:02:44","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/why-are-rails-model-association-results-not-naturally-activerecordrelations-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:02:44","modified_gmt":"2022-08-30T15:02:44","slug":"why-are-rails-model-association-results-not-naturally-activerecordrelations-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/why-are-rails-model-association-results-not-naturally-activerecordrelations-collection-of-common-programming-errors\/","title":{"rendered":"Why are Rails model association results not naturally ActiveRecord::Relations?-Collection of common programming errors"},"content":{"rendered":"<p>It is an <code>ActiveRecord::Relation<\/code>, but <em>Rails is intentionally lying to you<\/em>. You can see this already in the method calls, and continue to see it by calling <code>ancestors<\/code>, which includes a slew of ActiveRecord classes:<\/p>\n<pre><code>c1.articles.ancestors.select { |c| c.to_s =~ \/ActiveRecord\/ }.size  #=&gt; 35\n<\/code><\/pre>\n<p>which shows that it is very much <em>not<\/em> an <code>Array<\/code>.<\/p>\n<p>This happens because what you\u2019re getting back when calling <code>c1.articles<\/code> is an <code>ActiveRecord::Associations::CollectionProxy<\/code>*, which undefines <code>class<\/code> (along with many other methods). This means that <code>class<\/code> gets delegated via its <code>method_missing<\/code>, which sends it to <code>target<\/code>. As we can see, the class of <code>target<\/code> here is, in fact, <code>Array<\/code>:<\/p>\n<pre><code>c1.articles.target.class  #=&gt; Array\n<\/code><\/pre>\n<p>That is where <code>c1.articles.class<\/code> comes from. Nevertheless, it <em>is<\/em> an <code>ActiveRecord::Relation<\/code>.<\/p>\n<p><sub>* <em>We can verify that it is indeed an <code>ActiveRecord::Associations::CollectionProxy<\/code> by calling Ruby\u2019s original <code>class<\/code> method on the object in question: <code>Object.instance_method(:class).bind(c1.articles).call<\/code>. This is a nice trick to verify that the object is not trying to pretend to be of a different class.<\/em><\/sub><\/p>\n<p id=\"rop\"><small>Originally posted 2013-11-09 20:17:55. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>It is an ActiveRecord::Relation, but Rails is intentionally lying to you. You can see this already in the method calls, and continue to see it by calling ancestors, which includes a slew of ActiveRecord classes: c1.articles.ancestors.select { |c| c.to_s =~ \/ActiveRecord\/ }.size #=&gt; 35 which shows that it is very much not an Array. This [&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-521","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/521","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=521"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/521\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=521"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=521"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=521"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}