3

I am having trouble with what I thought would be a basic association.

I have a Game model and a Matchset model.

In the Game model is a list of games. The games are only listed once on the games table but they can belong to many Matchsets.

matchset.rb -

has_many :games

for game.rb I'm not sure what I would put. I don't want to put belongs_to because it belongs to many matchsets, not just one. And I don't think I would want to put has_and_belongs_to_many because matchsets shouldn't necessarily "belong to" games, but maybe I'm just looking at it wrong.

Example: Matchset 1 has games 1, 3, and 5. Matchset 2 has games 2 and 3. Matchset 3 has games 3, 4, and 5.

My background in with Oracle SQL and in my head the Matchset table would look something like this.

id | game_id 
1  | 1
1  | 3
1  | 5
2  | 2
2  | 3
3  | 3
3  | 4
3  | 5

Any help is appreciated.

mcnollster
  • 537
  • 1
  • 4
  • 14

1 Answers1

2

These relations should work for you:

class Game < ActiveRecord::Base
  has_many :game_match_set_relations
  has_many :match_sets, through: :game_match_set_relations

class MatchSet < ActiveRecord::Base
  has_many :game_match_set_relations
  has_many :games,  through: :game_match_set_relations

class GameMatchSetRelation < ActiveRecord::Base
  belongs_to :game
  belongs_to :match_set

  validates :game_id, presence: true
  validates :match_set_id, presence: true
MrYoshiji
  • 54,334
  • 13
  • 124
  • 117
  • I think the join model is unnecessary – mechanicalfish Nov 14 '13 at 19:16
  • I think it is, @mechanicalfish : the OP said `The games [...] but they can belong to many Matchsets.` – MrYoshiji Nov 14 '13 at 19:17
  • Yes, so `has_and_belongs_to_many` without the model. The join table itself is of course needed. – mechanicalfish Nov 14 '13 at 19:19
  • 1
    It is recomended to use a join model in all cases, it gives you a more flexible relation, you can add extra columns on the join model, better SQL queries, more precise validations, etc etc. (see the Rails Style Guide to get all the advantages) – MrYoshiji Nov 14 '13 at 19:21
  • Good reasoning, upvoting your answer. And thanks for the link, I didn't know something like that existed. – mechanicalfish Nov 14 '13 at 19:25
  • Thanks for the answers. So I need to create the GameMatchsetRelations table to join the two. Is there something special I need to do for creating a join table like this or is it like any other table? – mcnollster Nov 14 '13 at 19:44
  • It is like any other table, except that I strongly recommend you to add the validations in the join model ;-) – MrYoshiji Nov 14 '13 at 19:53
  • Do I need a controller for the new table? Usually I create scaffolds and the controller is created with it but not if I create just a table. Sorry for all the questions and thanks for answering. – mcnollster Nov 14 '13 at 20:04
  • Or I guess what I'm getting at is how can assign game IDs to matchsets now on a matchsets form? – mcnollster Nov 14 '13 at 20:30
  • Yes you could use this controller to contain the logic about the relations between the matchets and the games – MrYoshiji Nov 15 '13 at 23:44