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 ? -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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) -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 ? -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Not sure if you noticed the typo. Its ActiveRecord::Base (the "d" was missing in the answerer''s post). -- Raja Helder Oliveira wrote:> 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 ?-- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 24 September 2010 23:56, radhames brito <rbritom-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> class Game < ActiveRecor::Base > has_many :teams > > > class Team < ActiveRecor::Base > belongs_to :gameThat will only allow a team to be involved in one game, is that is what is required? Colin> > > > call designation of a game like this > > first get a game from the DB > > @game = Game.find(params[:id]) > > @designations = @game.teams.collect(&:designation) > > > > > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
clanlaw wrote:> On 24 September 2010 23:56, radhames brito <rbritom-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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? > > Colinbrito yes i notices the typo. colin, one game has two teams, team1_id and team2_id . I wanted this the right way or else i make a helper method that returns me the teams for each game, that''s not a big problem guys -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 25 September 2010 12:29, Helder Oliveira <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> clanlaw wrote: >> On 24 September 2010 23:56, radhames brito <rbritom-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 > > brito yes i notices the typo. > > colin, one game has two teams, team1_id and team2_id .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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.