具有相同列的 2 个表的 Rails 关联
我想知道... (1) 两个具有 2 个相同列的不同表的正确关联应该是什么 (2) 如何使用 for 循环在视图中显示用户列表
因此,一个表称为“参加”,有 2 列:“事件”和“事件”。用户 另一个表称为 NotAttending,有 2 列:Events & NotAttending。用户
class User < ActiveRecord::Base
has_many :attending
has_many :notattending
has_many :events, :through => :attending
has_many :events, :through => :notattending
end
class Event < ActiveRecord::Base
has_many :attending
has_many :notattending
has_many :users, :through => :attending
has_many :users, :through => :notattending
end
class Attending < ActiveRecord::Base
belongs_to :user
belongs_to :event
end
class Notattending < ActiveRecord::Base
belongs_to :user
belongs_to :event
end
如何在视图中显示出席和未出席的用户列表?我收到错误 undefined method users for nil:NilClass
<% for user in @attending.user %>
<%= user.name %></br>
<% end %>
谢谢!
I'm wondering...
(1) what should be the correct associations for 2 different tables both with 2 same columns
(2) how do i display the user list in views with for loop
So one table is called Attending and has 2 columns: Events & Users
Another table is called NotAttending and has 2 columns: Events & Users
class User < ActiveRecord::Base
has_many :attending
has_many :notattending
has_many :events, :through => :attending
has_many :events, :through => :notattending
end
class Event < ActiveRecord::Base
has_many :attending
has_many :notattending
has_many :users, :through => :attending
has_many :users, :through => :notattending
end
class Attending < ActiveRecord::Base
belongs_to :user
belongs_to :event
end
class Notattending < ActiveRecord::Base
belongs_to :user
belongs_to :event
end
How would I display the list of users for attending and notattending in the views? I get error undefined method users for nil:NilClass
<% for user in @attending.user %>
<%= user.name %></br>
<% end %>
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
旁白:为什么不将出席和非出席合并到一个具有三列的表中:事件、用户和 is_attending(如果出席则为 true,如果未出席则为 false)?
但无论如何,我们假设数据模型是固定的......
你不能使用 has_many :users 两次。您可以选择另一种方法:
ASIDE: why not combine Attending and Nonattending into one table with three columns, event, user, and is_attending (true if attending, false if not attending)?
But no matter, let's assume the data model is fixed...
You cannot use has_many :users twice. You could choose to make another method: