views:

33

answers:

1

I have a rails question that I cannot seem to wrap my head around.

I have an Invite model which will represent a name, address, number of people on the invitation, plus 1 or not, etc.

I also have an Event model which has name, location, and time of the event.

I would like to associate Invites to Events through something like a Schedule. I want to be able to create pre-defined Schedules as collections of Events and then associate an Invite to a specific Schedule.

So far I have the following.

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

If we have Events e1, e2, e3 and Invites i1, i2 and Schedules s1 has e1 and e2' and 's2 has e2 and e3 then I want to be able to associate Invite i1 with Schedule s1 and Invite i2 with Schedule s2.

I can get the Invites to Schedules relationship but the many-to-many Events-to-Schedules along with the Invites is currently confusing me. Any thoughts? Any other ways to think about this?

I ultimately want to be able to say invite.events and event.invites.

A: 

This is a little tricky to pull off, but not impossible. However you appear to be missing the join model for Events and Schedules. This is necessary to make the relationship work.

Also you'll need this plugin for the nested has_many :through relationship. of Events => Schedules => Invites. Once installed the following relationships will give you your desired effect.

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]
EmFi
i am going to try this later today but logically makes sense. Do you need a nested has_many :through relationship to relate Event to Invite as well b/c for Invite you have has_many :events, :through => :schedules, :source => :event_schedules but for Event you just have has_many :invites, :through => :schedules.
thomas
Yes. You could supply a :source option to the events => invites relationship. Which appears to work at a glance. Until you notice that you only ever get one invite returned per schedule.
EmFi