ror models relations - advice ?

Hello guys,

pretty simple:

table games: team1_id team2_id

table teams: id designation

which relations should i use to access the teams designations from the game model ?

class Game < ActiveRecor::Base has_many :teams

class Team < ActiveRecor::Base belongs_to :game

call designation of a game like this

first get a game from the DB

@game = Game.find(params[:id])

@designations = @game.teams.collect(&:designation)

radhames brito wrote:

class Game < ActiveRecor::Base has_many :teams

class Team < ActiveRecor::Base belongs_to :game

call designation of a game like this

first get a game from the DB

@game = Game.find(params[:id])

@designations = @game.teams.collect(&:designation)

tried and got error:

undefined method `teams' for nil:NilClass

isnt row names team1_id and team2_id a problem to call directly ?

Not sure if you noticed the typo. Its ActiveRecord::Base (the "d" was missing in the answerer's post).

-- Raja

Helder Oliveira wrote:

class Game < ActiveRecor::Base has_many :teams

class Team < ActiveRecor::Base belongs_to :game

That will only allow a team to be involved in one game, is that is what is required?

Colin

clanlaw wrote:

Yes, my question was how many games can a team be involved with. Radhames' suggestion would only allow each team to be involved in one game.

Colin

hum, well yes the relations is for a game to have many teams , but no for teams to be able to be involve in many game, is just that its not very clear what the requirements are and took this relation a a guide

table games:

team1_id team2_id

is a game having the id of 2 teams specifically.

i see you want to have teams involve in many games also, it appears you want to save the designations per game, because if you keep the designation in the teams table when a team is designation is edited it will change designations for pass games, so the best way to do it is to have a has many through relation with a designation table linking to the game and team table.

class Game < ActiveRecord::Base has_many :designations has_many :teams , :through => designations

class Team < ActiveRecord::Base has_many :designations has_many :games , :through => designations

class designation < ActiveRecord::Base

belongs_to :game belongs_to :team

Designations are now per team and per game so if you update a game or a team, designations in the past wont be affected. if you want to save more details per team and per game , change the name of the designation table to something more general and add all the fields that will be specific to the game played by those teams, like if is baseball, you can put the score, the homeruns and so on. You could call the table statistics or something like that.

you can call a designation for a game with

first get a game from the DB

@game = Game.find(params[:id])

@designations = @game.designations

to see how this kind of relations are made you can watch this railscasts

http://railscasts.com/episodes/47-two-many-to-many