{"id":997,"date":"2022-08-30T15:10:40","date_gmt":"2022-08-30T15:10:40","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/rails-has_many-through-with-conditions-on-aliased-association-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:10:40","modified_gmt":"2022-08-30T15:10:40","slug":"rails-has_many-through-with-conditions-on-aliased-association-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/rails-has_many-through-with-conditions-on-aliased-association-collection-of-common-programming-errors\/","title":{"rendered":"rails has_many :through with conditions on aliased association-Collection of common programming errors"},"content":{"rendered":"<p>This kind of goes along with my original question about <code>has_many :through<\/code> with conditions, but I&#8217;ve found another problem.<\/p>\n<p>Given this class:<\/p>\n<pre><code>class Contact &lt; AR\n  has_many :group_contacts\n  has_many :groups, :through =&gt; :group_contacts, :conditions =&gt; {:published =&gt; true}\n  has_many :anonymous_groups, :through =&gt; :group_contacts, :source =&gt; :group, :conditions =&gt; {:anonymous =&gt; true}\nend\n<\/code><\/pre>\n<p>My problem happens when I try to include the <code>anonymous_groups<\/code> with <code>contacts<\/code>:<\/p>\n<pre><code>Contact.includes(:anonymous_groups)\n<\/code><\/pre>\n<blockquote>\n<p>ActiveRecord::StatementInvalid: PGError: ERROR: missing FROM-clause entry for table &#8220;contacts&#8221;<\/p>\n<\/blockquote>\n<p>The reason for this is the generated sql is incorrect. It&#8217;s something akin to:<\/p>\n<pre><code>SELECT \"group_contacts\".\"id\" AS t0_r0 ... \"groups\".\"anonymous\" AS t1_r5 ... LEFT OUTER JOIN \"groups\" ON \"groups\".\"id\" = \"group_contacts\".\"group_id\" WHERE (\"group_contacts\".contact_id IN (...) AND (\"contacts\".\"anonymous\" = 'true'))\n<\/code><\/pre>\n<p>Paraphrased of course, but look at the final condition. It&#8217;s put the anonymous condition on <code>contacts<\/code> rather than <code>groups<\/code><\/p>\n<p>Now, we can alleviate this query error (which i&#8217;m sure is a bug) but doing something like:<\/p>\n<pre><code>has_many :anonymous_groups, :through =&gt; :group_challenges, :source =&gt; :group, :conditions =&gt; { :groups =&gt; {:anonymous =&gt; :true} }\n<\/code><\/pre>\n<p>This puts the condition on the correct table in sql, but when I try to build an anonymous group, I get this:<\/p>\n<pre><code>contact.anonymous_groups.build\n<\/code><\/pre>\n<blockquote>\n<p>ActiveRecord::UnknownAttributeError: unknown attribute: groups.anonymous<\/p>\n<\/blockquote>\n<p>So it works for querying but not for building. I&#8217;m quite certain this is a bug, but I&#8217;m wondering if anyone else has experienced this or has a workaround.<\/p>\n<ol>\n<li>\n<p>I think something like <code>:conditions =&gt; \"groups.anonymous = true\"<\/code> should work fine.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 23:12:02. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>This kind of goes along with my original question about has_many :through with conditions, but I&#8217;ve found another problem. Given this class: class Contact &lt; AR has_many :group_contacts has_many :groups, :through =&gt; :group_contacts, :conditions =&gt; {:published =&gt; true} has_many :anonymous_groups, :through =&gt; :group_contacts, :source =&gt; :group, :conditions =&gt; {:anonymous =&gt; true} end My problem happens [&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-997","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/997","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=997"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/997\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=997"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=997"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=997"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}