具有相同列的 2 个表的 Rails 关联

发布于 2024-12-07 06:31:25 字数 945 浏览 0 评论 0原文

我想知道... (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 技术交流群。

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

发布评论

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

评论(1

爱已欠费 2024-12-14 06:31:25

旁白:为什么不将出席和非出席合并到一个具有三列的表中:事件、用户和 is_attending(如果出席则为 true,如果未出席则为 false)?

但无论如何,我们假设数据模型是固定的......

你不能使用 has_many :users 两次。您可以选择另一种方法:

class User < ActiveRecord::Base
  has_many :attending
  has_many :notattending
  def events
    self.attending.map(&:events) + self.nonattending.map(&:events)
  end
end 

class Event < ActiveRecord::Base
  has_many :attending
  has_many :notattending
  def users
    self.attending.map(&:users) + self.nonattending.map(&:users)
  end
end 

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:

class User < ActiveRecord::Base
  has_many :attending
  has_many :notattending
  def events
    self.attending.map(&:events) + self.nonattending.map(&:events)
  end
end 

class Event < ActiveRecord::Base
  has_many :attending
  has_many :notattending
  def users
    self.attending.map(&:users) + self.nonattending.map(&:users)
  end
end 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文