Ruby on rails Rails:什么是最好的建模方法,它有很多关联

Ruby on rails Rails:什么是最好的建模方法,它有很多关联,ruby-on-rails,postgresql,activerecord,Ruby On Rails,Postgresql,Activerecord,假设我有两个模型Event和Person 很多人参加一个活动,一个人也可以参加很多活动 class Event < ActiveRecord::Base has_and_belongs_to_many :people end class Person < ActiveRecord::Base has_and_belongs_to_many :events end create_table "events_people", :id => false, :force =

假设我有两个模型
Event
Person

很多人参加一个活动,一个人也可以参加很多活动

class Event < ActiveRecord::Base
  has_and_belongs_to_many :people
end

class Person < ActiveRecord::Base
  has_and_belongs_to_many :events
end

create_table "events_people", :id => false, :force => true do |t|
  t.integer "event_id"
  t.integer "person_id"
end
class事件false,:force=>true do | t|
t、 整数“事件id”
t、 整数“person\u id”
结束
问题是一个事件是由一个或多个
演讲者
呈现的。因此,对于一个特定的
活动
,我们应该有
参加该活动,当然也应该有一个或多个
演讲者

我该怎么做?谢谢。

试试这个:

class Event < ActiveRecord::Base
  has_and_belongs_to_many :people
  has_and_belongs_to_many :speakers, :class_name => "Person"
end
class事件“Person”
结束

你会有一个
events\u speakers
join表,它将匹配
event\u id
person\u id
(它将指向你的
people
表中的id)。

我自己没有尝试过这个代码,但从我对activerecord关联的rails指南的理解来看,对我来说,在
events\u speaker
表中,它是
speaker\u id
,而不是
person\u id
。这是因为HABTM协会寻找一位演讲者。