{"id":1124,"date":"2022-08-30T15:12:47","date_gmt":"2022-08-30T15:12:47","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/self-referencing-has_many-through-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:12:47","modified_gmt":"2022-08-30T15:12:47","slug":"self-referencing-has_many-through-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/self-referencing-has_many-through-collection-of-common-programming-errors\/","title":{"rendered":"Self-referencing has_many, :through-Collection of common programming errors"},"content":{"rendered":"<p>Here&#8217;s my User model:<\/p>\n<pre><code>class User &lt; ActiveRecord::Base\n\n  has_many :friends, :class_name =&gt; 'Friendship', :dependent =&gt; :destroy\n\nend\n<\/code><\/pre>\n<p>Here&#8217;s my Friendship model:<\/p>\n<pre><code>class Friendship &lt; ActiveRecord::Base\n\n  belongs_to :user\n  belongs_to :friend, :class_name =&gt; 'User', :foreign_key =&gt; 'friend_id'\n\n  set_table_name :users_users\nend\n<\/code><\/pre>\n<p>Now, I have a boolean attribute in the User model called *is_awesome*.<\/p>\n<p>When I try to run this query:<\/p>\n<pre><code>User.find(1).friends.find(:all, :include =&gt; :users, :conditions =&gt; {:is_awesome =&gt; false})\n<\/code><\/pre>\n<p>I get the following error:<\/p>\n<pre><code>ActiveRecord::StatementInvalid: Mysql::Error: \nUnknown column 'users_users.is_awesome' in 'where clause': \nSELECT * FROM `users_users` \nWHERE (`users_users`.user_id = 1 AND (`users_users`.`is_awesome` = 0))\n<\/code><\/pre>\n<p>Any idea what&#8217;s going on?<\/p>\n<ol>\n<li>\n<p>You have to change :condition to refer to user table, it should looks something like this:<\/p>\n<pre><code>User.find(1).friends.find(:all, :include =&gt; :users, :conditions =&gt; \"users.is_awesome IS FALSE\")\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 23:30:39. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s my User model: class User &lt; ActiveRecord::Base has_many :friends, :class_name =&gt; &#8216;Friendship&#8217;, :dependent =&gt; :destroy end Here&#8217;s my Friendship model: class Friendship &lt; ActiveRecord::Base belongs_to :user belongs_to :friend, :class_name =&gt; &#8216;User&#8217;, :foreign_key =&gt; &#8216;friend_id&#8217; set_table_name :users_users end Now, I have a boolean attribute in the User model called *is_awesome*. When I try to run [&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-1124","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1124","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=1124"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1124\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1124"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1124"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1124"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}