has_many 关联表令人困惑

发布于 2024-12-10 10:33:51 字数 1568 浏览 0 评论 0原文

我的目标是打印指定城市和类别的用户列表及其姓名、地址和俱乐部名称。名称和地址显示正确,但当我添加俱乐部名称时,显示 #的未定义方法成员资格。我知道这是因为俱乐部处于不同的模型中,所以问题是我如何访问俱乐部名称?我对复杂的 has_many 关系真的很浅薄。任何人都可以进行必要的更正吗拜托,这就是我走了多远。提前谢谢你

模型

class User < ActiveRecord::Base
  has_many :memberships 
  has_many : clubs,:through =>:memberships
  belongs_to :category
  belongs_to :city
end

class Club < ActiveRecord::Base
  has_many :users 
  has_many :memberships 
  has_many : users ,:through =>:memberships
  belongs_to :city
end

class Membership < ActiveRecord::Base
  belongs_to :user
  belongs_to :club
end

class City < ActiveRecord::Base
  has_many :clubs 
  has_many :users  
end

class Category < ActiveRecord::Base
  has_many :users
end

控制器

  @users=User.where("category_id =? AND   city_id=?",Category.find(2),session[:city_id])
  @[email protected]
  @[email protected] #this gives undefined method membership

查看

    <% @user.each do |user| %>
   <%= user.username %>
   <%= user.address %>
   <%= @club.name %> #attribute name is in the club model not membership
   <%end%>

路线

       devise_for :users
      resources :city,:club,:category

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 #<Enumerator:0xa5509b0>.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

MODELS

class User < ActiveRecord::Base
  has_many :memberships 
  has_many : clubs,:through =>:memberships
  belongs_to :category
  belongs_to :city
end

class Club < ActiveRecord::Base
  has_many :users 
  has_many :memberships 
  has_many : users ,:through =>:memberships
  belongs_to :city
end

class Membership < ActiveRecord::Base
  belongs_to :user
  belongs_to :club
end

class City < ActiveRecord::Base
  has_many :clubs 
  has_many :users  
end

class Category < ActiveRecord::Base
  has_many :users
end

CONTROLLER

  @users=User.where("category_id =? AND   city_id=?",Category.find(2),session[:city_id])
  @[email protected]
  @[email protected] #this gives undefined method membership

VIEW

    <% @user.each do |user| %>
   <%= user.username %>
   <%= user.address %>
   <%= @club.name %> #attribute name is in the club model not membership
   <%end%>

ROUTE

       devise_for :users
      resources :city,:club,:category

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挽清梦 2024-12-17 10:33:51

下面的行返回一个数组,而不是您期望的数组,即 User 对象。

@[email protected]

如果视图中显示的用户属于不同的俱乐部,您应该直接在视图中访问他们。如果需要,您可以立即加载俱乐部。

在您的控制器中

@users=User.where(:category_id => Category.find(2), :city_id=>session[:city_id]).
  include(:clubs)

在您的视图中

<% @users.each do |user| %>
  <%= user.username %>
  <%= user.address %>
  <% user.clubs.each do |club| %>
    <%= club.name %>
  <%end%>  
<%end%>

The line below returns an array instead of what you are expecting, i.e. User object.

@[email protected]

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.

In your controller

@users=User.where(:category_id => Category.find(2), :city_id=>session[:city_id]).
  include(:clubs)

In your view

<% @users.each do |user| %>
  <%= user.username %>
  <%= user.address %>
  <% user.clubs.each do |club| %>
    <%= club.name %>
  <%end%>  
<%end%>
过气美图社 2024-12-17 10:33:51

首先,你在俱乐部课上有一个错误。你不能有两个同名的关系。第二个将覆盖第一个。

has_many : users ,:through =>:memberships

应该是

has_many :members, :through =>:memberships, :foreign_key => 'user_id'

搜索应该是:

@users = User.where(:category_id => 2, :city_id => 3).include(:clubs)

或者您想要搜索的任何值。视图是:

<% @user.each do |user| %>
  <%= user.username %>
  <%= user.address %>
  <%= user.club.name %> #attribute name is in the club model not membership
<%end%>

您的路线很好。

First of all, you have an error in Club class. You cant have 2 relations with the same name. Second one will override first.

has_many : users ,:through =>:memberships

should be

has_many :members, :through =>:memberships, :foreign_key => 'user_id'

The search should be:

@users = User.where(:category_id => 2, :city_id => 3).include(:clubs)

Or whatever it values you want to search by.The view is:

<% @user.each do |user| %>
  <%= user.username %>
  <%= user.address %>
  <%= user.club.name %> #attribute name is in the club model not membership
<%end%>

Your routes are fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文