您需要建立 2 个关联 - 一个链接到创建的事件,一个链接到订阅的事件:
create_table :event_subscriptions do |t|
t.references :subscribed_user
t.references :subscribed_event
end
create_table :events do |t|
t.references :user
# other fields
end
class User < ActiveRecord::Base
has_many :events # created Events
has_many :event_subscriptions, :foreign_key => :subscribed_user_id
has_many :subscribed_events, :through => :event_subscriptions # subscribed Events
end
class EventSubscription < ActiveRecord::Base
belongs_to :subscribed_user, :class_name => 'User'
belongs_to :subscribed_event, :class_name => 'Event'
end
class Event < ActiveRecord::Base
belongs_to :user # the creating User
has_many :event_subscriptions, :foreign_key => :subscribed_event_id
has_many :subscribed_users, :through => :event_subscriptions # subscribed Users
end