{"id":1237,"date":"2022-08-30T15:14:40","date_gmt":"2022-08-30T15:14:40","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/10\/rais-has_many-through-query-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:14:40","modified_gmt":"2022-08-30T15:14:40","slug":"rais-has_many-through-query-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/rais-has_many-through-query-collection-of-common-programming-errors\/","title":{"rendered":"Rais has_many &amp; through query-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m using Rails 2.3 and I have 3 tables, which manages user permissions to buildings. The mygroups table is the list of groups users and buildings belong to. The mygroupsbuildings is the list of buildings with their associated group. The mygroupsusers is the list of users with their associated group.<\/p>\n<pre><code>create_table \"mygroups\", :force =&gt; true do |t|\n  t.string   \"name\"\n  t.integer  \"user_id\"\n  t.string   \"user_name\"\n  t.integer  \"permissions\"\nend\n\ncreate_table \"mygroupbuildings\", :force =&gt; true do |t|\n  t.integer  \"mygroup_id\"\n  t.integer  \"building_id\"\nend\n\ncreate_table \"mygroupusers\", :force =&gt; true do |t|\n  t.integer  \"mygroup_id\"\n  t.integer  \"user_id\"\n  t.string   \"user_name\"\nend\n<\/code><\/pre>\n<p>I would like to create a query that finds out if a particular user_id is a member of the same group as a particular building_id.<\/p>\n<p>I have setup the following associations:<\/p>\n<pre><code>class Mygroup &lt; ActiveRecord::Base  \n  has_many   :mygroupusers\n  has_many   :mygroupbuildings\nend\n\nclass Mygroubuilding &lt; ActiveRecord::Base  \n  belongs_to :mygroup\n  has_many   :mygroupusers, :through =&gt; :mygroup\nend\n\nclass Mygroupuser &lt; ActiveRecord::Base\n  belongs_to :mygroup\n  has_many   :mygroupbuildings, :through =&gt; :mygroup\nend\n<\/code><\/pre>\n<p>I wrote the following query:<\/p>\n<pre><code>Mygroupuser.find(:all,\n                 :include =&gt; :mygroupbuildings,\n                 :select =&gt; \"mygroupbuildings.building_id, mygroupusers.user_id\",\n                 :conditions =&gt; [\"mygroupbuildings.building_id = '#{building.id}' AND mygroupusers.user_id = '#{current_user.user_id}'\"])      \n<\/code><\/pre>\n<p>This give me the following error:<\/p>\n<p>ActiveRecord::StatementInvalid (Mysql::Error: Unknown column &#8216;mygroups.mygroup_id&#8217; in &#8216;on clause&#8217;: SELECT <code>mygroupusers<\/code>.<code>id<\/code> AS t0_r0, <code>mygroupusers<\/code>.<code>mygroup_id<\/code> AS t0_r1, <code>mygroupusers<\/code>.<code>user_id<\/code> AS t0_r2, <code>mygroupusers<\/code>.<code>user_name<\/code> AS t0_r3, <code>mygroupusers<\/code>.<code>created_at<\/code> AS t0_r4, <code>mygroupusers<\/code>.<code>updated_at<\/code> AS t0_r5, <code>mygroupbuildings<\/code>.<code>id<\/code> AS t1_r0, <code>mygroupbuildings<\/code>.<code>mygroup_id<\/code> AS t1_r1, <code>mygroupbuildings<\/code>.<code>coursewalk_id<\/code> AS t1_r2, <code>mygroupbuildings<\/code>.<code>created_at<\/code> AS t1_r3, <code>mygroupbuildings<\/code>.<code>updated_at<\/code> AS t1_r4 FROM <code>mygroupusers<\/code> LEFT OUTER JOIN <code>mygroups<\/code> ON (<code>mygroupusers<\/code>.<code>id<\/code> = <code>mygroups<\/code>.<code>mygroup_id<\/code>) LEFT OUTER JOIN <code>mygroupbuildings<\/code> ON (<code>mygroupbuildings<\/code>.<code>mygroup_id<\/code> = <code>mygroups<\/code>.<code>id<\/code>) WHERE (mygroupbuildings.building_id = &#8216;2&#8217; AND mygroupusers.user_id = &#8217;55&#8217;) ):<\/p>\n<p>Thanks,<\/p>\n<p>Nick,<\/p>\n<ol>\n<li>\n<p>This is how the query needs to be setup to return the group that contains the queried user_id and building_id:<\/p>\n<pre><code>Mygroup.find(:all,\n             :include =&gt; [:mygroupbuildings, :mygroupusers],\n             :conditions =&gt; [\"mygroupbuildings.building_id = #{building.id} AND mygroupusers.user_id = #{current_user.user_id}\"])      \n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-10 00:11:29. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I&#8217;m using Rails 2.3 and I have 3 tables, which manages user permissions to buildings. The mygroups table is the list of groups users and buildings belong to. The mygroupsbuildings is the list of buildings with their associated group. The mygroupsusers is the list of users with their associated group. create_table &#8220;mygroups&#8221;, :force =&gt; true [&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-1237","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1237","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=1237"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1237\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1237"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1237"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1237"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}