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.
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.