2009-12-09 75 views
0

我有一个问题,我似乎无法包裹我的头。Rails邀请参加活动

我有一个邀请,模型,将代表的姓名,地址,的人的邀请,加1或者不号码等

我也有有名称,地点的事件模型,和时间事件。

我想通过像Schedule这样的东西来将Invites to Events关联起来。我希望能够创建预定义的计划作为事件的集合,然后将邀请与特定的计划相关联。

到目前为止,我有以下几点。

class Invite < ActiveRecord::Base 
    belongs_to :schedule 
    has_many :events, :through => :schedules 

    #a schedule_id column exists in the invites table 
end 

class Event < ActiveRecord::Base 
    has_many :schedules 
    has_many :invites, :through => :schedules 
end 

class Schedule < ActiveRecord::Base 
    has_many :events 
    has_many :invites 
end 

如果我们有活动e1, e2, e3并邀请i1, i2和时间表s1 has e1 and e2' and 's2 has e2 and e3然后我希望能够邀请相关联与i1附表s1和附表s2邀请i2

我可以得到Invites到Schedules的关系,但是与Invites一起的多对多Events-to-Schedules目前令我困惑。有什么想法吗?任何其他方式来思考这个?

我最终希望能够说invite.eventsevent.invites

+0

我说得对吗;你有邀请有多对多的事件和时间表是关联表?包含关于事件周围的关联的元数据的时间表等。 – 2009-12-09 04:41:02

+0

是的......听起来正确。 – thomas 2009-12-09 12:32:14

回答

0

这有点棘手,但并非不可能。但是,您似乎错过了事件和计划的加入模型。这是使关系有效的必要条件。

此外,您将需要此plugin为嵌套的has_many:through关系。事件=>计划=>邀请。一旦安装,以下关系将会给你你想要的效果。

class Invite < ActiveRecord::Base 
    belongs_to :schedule 
    has_many :events, :through => :schedules, :source => :event_shedules 

    #a schedule_id column exists in the invites table 
end 

class Event < ActiveRecord::Base 
    has_many :event_schedules 
    has_many :schedules, :through => :event_schedules 
    has_many :invites, :through => :schedules 
end 

class EventSchedules < ActiveRecord::Base 
    belongs_to :event 
    belongs_to :schedules 
end 

class Schedule < ActiveRecord::Base 
    has_many :event_scheudles 
    has_many :events, :through => :event_schedules 
    has_many :invites 
end 

@s1 = Schedule.create 
@s2 = Schedule.create 
@e1 = Event.create 
@e2 = Event.create 
@e3 = Event.create 
@s1.events << [@e1,@e2] 
@s2.events << [@e2, @e3] 
@i1 = @s1.invites.create 
@i2 = @s2.invites.create 

@s1.invites # => [@i1] 
@s1.events # => [@e1,@e2] 
@s2.invites # => [@i2] 
@s2.events # => [@e2,@e3] 


@e1.invites # => [@i1] #not possible without the plugin 
@e2.invites # => [@i1,@i2] #not possible without the plugin 
@e3.invites # => [@i2] #not possible without the plugin 

@i1.events # => [@e1, @e2] 
@i2.events # => [@e2, @e3] 
+0

我今天晚些时候会尝试,但在逻辑上是有道理的。你需要一个嵌套的has_many:through关系来将Event与Invite关联起来,并且对于Invite你有has_many:events,:through =>:schedules,:source =>:event_schedules,但是对于刚刚拥有的事件has_many:invites, :通过=>:时间表。 – thomas 2009-12-09 12:35:27

+0

是的。您可以为events =>邀请关系提供:source选项。这似乎一目了然。直到您注意到您只收到一个按计划返回的邀请。 – EmFi 2009-12-09 15:02:19

相关问题