{"id":1309,"date":"2022-08-30T15:15:27","date_gmt":"2022-08-30T15:15:27","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/10\/undefined-method-on-has_many-through-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:15:27","modified_gmt":"2022-08-30T15:15:27","slug":"undefined-method-on-has_many-through-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/undefined-method-on-has_many-through-collection-of-common-programming-errors\/","title":{"rendered":"Undefined Method on has_many :through-Collection of common programming errors"},"content":{"rendered":"<p>I have three models:<\/p>\n<pre><code>Class Project &lt; ActiveRecord::Base\n  has_many :tasks\n  has_many :tags, :through =&gt; :tasks\nend\n\nClass Tasks &lt; ActiveRecord::Base\n  belongs_to :project\n  has_and_belongs_to_many :tags\nend\n\nClass Tags &lt; ActiveRecord::Base\n  has_and_belongs_to_many :tasks\n  has_many :projects, :through =&gt; :tasks\n<\/code><\/pre>\n<p>When I open up console, I can get my Project and Task information as expected:<\/p>\n<pre><code>Tag.find(1).projects\nTag.find(1).tasks\n<\/code><\/pre>\n<p>If I want, I can get all the tasks for each project regardless of the tag:<\/p>\n<pre><code>Project.find(1).tasks\n<\/code><\/pre>\n<p>For whatever reason, I can&#8217;t access tasks if I get projects by tag&#8230; something = Tag.find(1).projects something.tasks<\/p>\n<p>&#8230;I get the error:<\/p>\n<pre><code>undefined method `tasks' for #\n<\/code><\/pre>\n<p>I&#8217;ve looked for a couple hours and can&#8217;t find anything that corrects this problem. Based on everything I&#8217;ve found, it should be working&#8230; but it&#8217;s not.<\/p>\n<p>I&#8217;m using Rails 3.2.3.<\/p>\n<ol>\n<li>\n<p>Shouldn&#8217;t <code>Tag.find(1).tasks<\/code> give you the same result?<\/p>\n<p>Anyway, the problem you&#8217;re facing is that you&#8217;re trying to retrieve an association from a <code>Relation<\/code> object instead of an instance of your model. <code>Relations<\/code> can be used to chain query conditions, but you can&#8217;t directly reference associations from them. So, to get your example working, you&#8217;d need to do<\/p>\n<pre><code>p = Tag.find(1).projects.includes(:tasks)\n<\/code><\/pre>\n<p>Then reference tasks like this: <code>p[0].tasks<\/code>.<\/p>\n<p>However I&#8217;d just make sure that <code>Tag.find(1).tasks<\/code> will generate the same SQL and ultimately return the same collection of <code>tasks<\/code>.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-10 00:15:58. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I have three models: Class Project &lt; ActiveRecord::Base has_many :tasks has_many :tags, :through =&gt; :tasks end Class Tasks &lt; ActiveRecord::Base belongs_to :project has_and_belongs_to_many :tags end Class Tags &lt; ActiveRecord::Base has_and_belongs_to_many :tasks has_many :projects, :through =&gt; :tasks When I open up console, I can get my Project and Task information as expected: Tag.find(1).projects Tag.find(1).tasks If I [&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-1309","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1309","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=1309"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1309\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1309"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1309"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1309"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}