I’m trying to create the best modeling strategy for my application and am having a problem understanding the most efficient solution.
At the top layer we have Gamers.
Gamers can have many Events.
Events have many Participants (who are Gamers).
There can be any number of Participants in an Event, which is what is throwing me off. If they were a set number of Participants I could just have them as columns inside of the Events model with a foreign key to Gamers, but I’m not sure how to setup associations for this.
My testing has them setup like so
rails gen. model Gamer ..
rails gen model Event ..
rails gen model Participants .. event:references
So now…
Event has_many :participants
Participants belongs_to :event
The problem I can’t figure out is how to link Participants to their Gamer row?
I believe it is something similar the following, but the examples in the Rails guide don’t fit my example exactly…
Gamer has_many :event_participants, :through => :events
I’d approach this with some relation naming changes and a join table:
And in your DB, you’ll need the join table
events_gamers, withevent_idandgamer_idcolumns. No model, though – Rails will take care of everything behind the scenes.You really only need the two models. Unless the relation “gamer G participated in event E” needs more data attached to it (say, “and brought chips!”) – then it would warrant its own model (I’d call it Participation – Participant is a little misleading), and the
has_and_belongs_to_manyrelationships would becomehas many :throughs.Hope this helps!