I am new to Rails just been learning the last month or so. I have a question regarding setting up model associations. I have sort of hit a spot where I think I need to go back and make changes to what I've already done. Here is the scenario.
I am creating an application for bands to list upcoming events.
There are a few important factors
Currently this is how its set up:
class Event < ActiveRecord::Base
belongs_to :venue
scope :upcoming, where('date >= ?', Date.today)
end
class Venue < ActiveRecord::Base
has_many :events
accepts_nested_attributes_for :events
end
I haven't really done much with the artist model yet as I was just focusing on getting the events in to the system and listed properly for 1 artist which was hardcoded in.
Essentially right now how it works is the user goes to the create an event form, they select the date and the next line is venue name. The venue name form autocompletes with the Venue Name + City from the DB. If the user is creating a event at a venue already in the system it essentially saves the Event info via the Venue model as a nested attribute. If the venue is not in the system additional input fields for location data appear and then a new venue is created and associated with that event.
It begins to get a bit tricky when I start to think about the best way to have artists share events and maintain particular details about events that are specific to that artist only. I could just treat every event as an individual event and it would be easy but then I am not sure how I would be able to say this artist is performing with this artists on this date aside from the artists inputting those names directly into 1 field. I also then lose a lot of the magic of the ActiveRecords with respect to which artists performed with which other artists where, etc.
As of now there is just 1 model for events, but i am thinking maybe it might be best to keep 1 central model for just the event basics that aren't shared by multiple bands like venue, city, state, etc and then link those to a secondary event_details model for band specific information.
Would you suggest the multiple models or is there a better way for me to link bands playing multiple events together than creating an event detail model linking back to the 1 main event that multiple artists share? Should I just create some type of unique event identifier on the events table so that I am treating an event with a different ID as the same event another band is playing? Only issue I see with that is then I am going to be doubling up on a lot of that data that is in my database...I essentially want to be able to while looking at event data say ok on this date the artist is playing this show, at this venue, with these other bands with out having to just simple make an "other_artists" column where the bands would simply list artists they are performing with.
sorry i hope that makes sense...thanks in advance for any advice!
I think your real issue here is that you are going to need to handle a many-to-many relationship between artists and events. Events will have many artists booked, and artists will have many events on their schedule.
I would create a "Booking" class that specifically ties one artist to one event. This booking will be a good place to hold any special requirements or special arrangements for that artist playing at that event. Using ActiveRecord's "has_many :through" relationship will make this very straight forward and give you a lot of flexibility in how you can query the data. Here is how I would do it:
class Event < ActiveRecord::Base
belongs_to :venue
has_many :artists, :through => :bookings
scope :upcoming, where('date >= ?', Date.today)
end
class Venue < ActiveRecord::Base
has_many :events
accepts_nested_attributes_for :events
end
class Artist < ActiveRecord::Base
has_many :events, :through => :bookings
end
class Booking < ActiveRecord::Base
belongs_to :artist
belongs_to :event
attr_accessible :special_requirements, :special_arrangements
end
Some examples the flexibility you get in querying data via these relationships:
@venue.events
@venue.events.where(:id => specific_event.id).first.artists
@event.artists
@artist.events
@event.bookings each do |b|
b.artist
b.special_requirements
end
Hope this helps.