我有一个rails的问题,我似乎无法理解。
我有一个Invite模型,它将代表一个姓名,地址,邀请上的人数,加1或不加,等等。
我还有一个事件模型,它有事件的名称、位置和时间。
我想通过诸如日程安排之类的方式将邀请与事件相关联。我希望能够创建作为事件集合的预定义计划,然后将Invite关联到特定计划。
到目前为止,我有以下几点。
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如果我们有Events e1, e2, e3、Invite i1, i2和Schedules s1 has e1 and e2' and 's2 has e2 and e3,那么我希望能够将Invite i1与Schedule s1相关联,并将Invite i2与Schedule s2相关联。
我可以获得邀请到日程安排的关系,但多对多事件到日程安排以及邀请目前让我感到困惑。有什么想法吗?有没有其他方式来思考这个问题?
我最终希望能够说出invite.events和event.invites。
发布于 2009-12-09 13:46:29
这有点棘手,但也不是不可能。但是,您似乎缺少事件和计划的联接模型。这是维持这种关系所必需的。
此外,您还需要为嵌套的has_many :through关系使用此plugin。=>邀请的活动=> Schedules。安装后,以下关系将为您提供所需的效果。
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]https://stackoverflow.com/questions/1871335
复制相似问题