{"id":867,"date":"2022-08-30T15:08:30","date_gmt":"2022-08-30T15:08:30","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/has_many-associations-table-confusing-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:08:30","modified_gmt":"2022-08-30T15:08:30","slug":"has_many-associations-table-confusing-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/has_many-associations-table-confusing-collection-of-common-programming-errors\/","title":{"rendered":"has_many associations table confusing-Collection of common programming errors"},"content":{"rendered":"<p>My goal here is to print list of users from specified city and category with their names, address and club name.Name and address display correctly but when i add club name,says <code>undefined method memberships for #<\/code>.I know this is because Club is in different model ,So the question is how do i access club name?I am really shallow in complex has_many realtionships.Can anybody make necesary corrections please,This is how far i got.Thank you in advance<\/p>\n<p>MODELS<\/p>\n<pre><code>class User &lt; ActiveRecord::Base\n  has_many :memberships \n  has_many : clubs,:through =&gt;:memberships\n  belongs_to :category\n  belongs_to :city\nend\n\nclass Club &lt; ActiveRecord::Base\n  has_many :users \n  has_many :memberships \n  has_many : users ,:through =&gt;:memberships\n  belongs_to :city\nend\n\nclass Membership &lt; ActiveRecord::Base\n  belongs_to :user\n  belongs_to :club\nend\n\nclass City &lt; ActiveRecord::Base\n  has_many :clubs \n  has_many :users  \nend\n\nclass Category &lt; ActiveRecord::Base\n  has_many :users\nend\n<\/code><\/pre>\n<p>CONTROLLER<\/p>\n<pre><code>  @users=User.where(\"category_id =? AND   city_id=?\",Category.find(2),session[:city_id])\n  @user=@users.collect\n  @club=@user.memberships #this gives undefined method membership\n<\/code><\/pre>\n<p>VIEW<\/p>\n<pre><code>    \n   \n   \n    #attribute name is in the club model not membership\n   \n<\/code><\/pre>\n<p>ROUTE<\/p>\n<pre><code>       devise_for :users\n      resources :city,:club,:category\n<\/code><\/pre>\n<ol>\n<li>\n<p>The line below returns an array instead of what you are expecting, i.e. User object.<\/p>\n<pre><code>@user=@users.collect\n<\/code><\/pre>\n<p>If users shown in the view belong to different clubs you should access them directly in the view. You can eager load the clubs if needed.<\/p>\n<p><strong>In your controller<\/strong><\/p>\n<pre><code>@users=User.where(:category_id =&gt; Category.find(2), :city_id=&gt;session[:city_id]).\n  include(:clubs)\n<\/code><\/pre>\n<p><strong>In your view<\/strong><\/p>\n<pre><code>\n  \n  \n  \n    \n    \n\n<\/code><\/pre>\n<\/li>\n<li>\n<p>First of all, you have an error in Club class. You cant have 2 relations with the same name. Second one will override first.<\/p>\n<pre><code>has_many : users ,:through =&gt;:memberships\n<\/code><\/pre>\n<p>should be<\/p>\n<pre><code>has_many :members, :through =&gt;:memberships, :foreign_key =&gt; 'user_id'\n<\/code><\/pre>\n<p>The search should be:<\/p>\n<pre><code>@users = User.where(:category_id =&gt; 2, :city_id =&gt; 3).include(:clubs)\n<\/code><\/pre>\n<p>Or whatever it values you want to search by.The view is:<\/p>\n<pre><code>\n  \n  \n   #attribute name is in the club model not membership\n\n<\/code><\/pre>\n<p>Your routes are fine.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 22:49:54. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>My goal here is to print list of users from specified city and category with their names, address and club name.Name and address display correctly but when i add club name,says undefined method memberships for #.I know this is because Club is in different model ,So the question is how do i access club name?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-867","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/867","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=867"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/867\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=867"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=867"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=867"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}