首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails Associate邀请参加活动

Rails Associate邀请参加活动
EN

Stack Overflow用户
提问于 2009-12-09 10:58:35
回答 1查看 351关注 0票数 0

我有一个rails的问题,我似乎无法理解。

我有一个Invite模型,它将代表一个姓名,地址,邀请上的人数,加1或不加,等等。

我还有一个事件模型,它有事件的名称、位置和时间。

我想通过诸如日程安排之类的方式将邀请与事件相关联。我希望能够创建作为事件集合的预定义计划,然后将Invite关联到特定计划。

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

代码语言:javascript
复制
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.eventsevent.invites

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2009-12-09 13:46:29

这有点棘手,但也不是不可能。但是,您似乎缺少事件和计划的联接模型。这是维持这种关系所必需的。

此外,您还需要为嵌套的has_many :through关系使用此plugin。=>邀请的活动=> Schedules。安装后,以下关系将为您提供所需的效果。

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1871335

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档