{"id":1283,"date":"2022-08-30T15:15:14","date_gmt":"2022-08-30T15:15:14","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/10\/rails-has_many-through-with-conditions-and-building-associations-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:15:14","modified_gmt":"2022-08-30T15:15:14","slug":"rails-has_many-through-with-conditions-and-building-associations-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/rails-has_many-through-with-conditions-and-building-associations-collection-of-common-programming-errors\/","title":{"rendered":"Rails has_many :through with conditions and building associations-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m having problems building an association that is a <code>has_many :through<\/code> with conditions. I have this model:<\/p>\n<pre><code>class Contact &lt; AR\n  has_many :group_contacts\n  has_many :groups, :through =&gt; :group_contacts, :conditions =&gt; {:groups =&gt; {:published =&gt; true}}\nend\n<\/code><\/pre>\n<p>problem happens when I try to instantiate a group from a contact. With the above syntax, I get an error:<\/p>\n<pre><code>contact.groups.build\n=&gt; ActiveRecord::UnknownAttributeError: unknown attribute: groups\n<\/code><\/pre>\n<p>But when I use the following syntax it works:<\/p>\n<pre><code>has_many :groups, :through =&gt; :group_contacts, :conditions =&gt; ['groups.published = ?', true]\n\ncontact.groups.build\n=&gt; #\n<\/code><\/pre>\n<p>I see a reference to the exact problem in this question. It is said a ticket would be filed for this bug (back in pre- rails 3 versions). I can&#8217;t find anything however on <code>rails 3.0.x<\/code>.<\/p>\n<p>I&#8217;m using 3.0.8. Has anyone else found this issue?<\/p>\n<p><em>Further Notes<\/em>:<\/p>\n<p>I&#8217;ve also found that when I&#8217;m building groups, it actually ignores my conditions on the association when building. The only reason my above build had <code>published =&gt; true<\/code> is because it&#8217;s the default in the db.<\/p>\n<p>This seems like a regression, can anyone else verify this?<\/p>\n<ol>\n<li>\n<pre><code>has_many :groups, :through =&gt; :group_contacts, :conditions =&gt; {:published =&gt; true}\n<\/code><\/pre>\n<p>or<\/p>\n<pre><code>has_many :groups, :through =&gt; :group_contacts, :conditions =&gt; {\"groups.published\" =&gt; true}\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-10 00:14:19. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I&#8217;m having problems building an association that is a has_many :through with conditions. I have this model: class Contact &lt; AR has_many :group_contacts has_many :groups, :through =&gt; :group_contacts, :conditions =&gt; {:groups =&gt; {:published =&gt; true}} end problem happens when I try to instantiate a group from a contact. With the above syntax, I get an [&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-1283","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1283","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=1283"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1283\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1283"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1283"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1283"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}