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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
下面的行返回一个数组,而不是您期望的数组,即 User 对象。
如果视图中显示的用户属于不同的俱乐部,您应该直接在视图中访问他们。如果需要,您可以立即加载俱乐部。
在您的控制器中
在您的视图中
The line below returns an array instead of what you are expecting, i.e. User object.
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
In your view
首先,你在俱乐部课上有一个错误。你不能有两个同名的关系。第二个将覆盖第一个。
应该是
搜索应该是:
或者您想要搜索的任何值。视图是:
您的路线很好。
First of all, you have an error in Club class. You cant have 2 relations with the same name. Second one will override first.
should be
The search should be:
Or whatever it values you want to search by.The view is:
Your routes are fine.