Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何处理一个有很多且属于的关系_Ruby On Rails_Activerecord - Fatal编程技术网

Ruby on rails 如何处理一个有很多且属于的关系

Ruby on rails 如何处理一个有很多且属于的关系,ruby-on-rails,activerecord,Ruby On Rails,Activerecord,我有用户,他们可以制作事件。这些是组织者 其他用户s可以作为与会者前往这些活动s 我应该如何处理以下关系 用户: 活动: belongs_to :user #how to do the organizer and attendees distinction? belongs_to :organizer, class_name: "User" has_and_belongs_to_many :attendees, class_name: "User" 每个模型上都需要两个不同的关系,它们的名称

我有
用户
,他们可以制作
事件
。这些是组织者

其他
用户
s可以作为与会者前往这些
活动
s

我应该如何处理以下关系

用户:

活动:

belongs_to :user #how to do the organizer and attendees distinction?
belongs_to :organizer, class_name: "User"
has_and_belongs_to_many :attendees, class_name: "User"

每个模型上都需要两个不同的关系,它们的名称不同,但类相同。此外,与会者和活动之间存在多对多关系

使用者

活动:

belongs_to :user #how to do the organizer and attendees distinction?
belongs_to :organizer, class_name: "User"
has_and_belongs_to_many :attendees, class_name: "User"

我建议创建一个
event\u attends
表,并将
user\u id
event\u id
列放在那里,将
user\u id
event\u attendment\u id
放在
events
表中。然后您可以创建如下关联:

class User < ActiveRecord::Base
  has_many :event_attends
  has_many :events
end

class Event < ActiveRecord::Base
  belongs_to :user
  has_many :event_attends
end

class EventAttend < ActiveRecord::Base
  belongs_to :user
  belongs_to :event
end
class用户
每个活动只有一名与会者?这听起来像是一个孤独、悲伤的事件。不妨添加一个
has\u many:attenders,through::event\u attendes,source::user
,作为衡量标准!