![](/img/trans.png)
[英]Using Has_and_belongs_to_many association in multi model view rails
[英]Rails: what is the best way to model this has_and_belongs_to_many association
假设我有两个模型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
问题是一个或多个speakers
主持了一个活动。 因此,对于一个特定的event
,我们应该有people
谁参加该事件和一个或多个speakers
谁是当然的,人太多。
我怎么做 ? 谢谢。
尝试这个:
class Event < ActiveRecord::Base
has_and_belongs_to_many :people
has_and_belongs_to_many :speakers, :class_name => "Person"
end
你将有一个events_speakers
连接表,将匹配event_id
和person_id
(这将指向中的ID people
表)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.