Hello, I''m having some troubles today with Ruby... I do @league = League.find(params[:id], :include => [ :teams, :games ]) then @league.teams.each do |t| and it goes 2 times through the teams list ??? even if @league.teams.size = 2. Does anyone have a clue where it can come from?? Thank you for you help, Thierry -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Could the inclusion of the games records be multiplying the number of teams that get returned maybe? Any difference if you take :games out of that :include array there? -----Original Message----- From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org [mailto:rubyonrails-talk@googlegroups.com] On Behalf Of Thierry Delbart Sent: Friday, October 24, 2008 2:24 PM To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Subject: [Rails] ''each'' loop 2 times trough all records Hello, I''m having some troubles today with Ruby... I do @league = League.find(params[:id], :include => [ :teams, :games ]) then @league.teams.each do |t| and it goes 2 times through the teams list ??? even if @league.teams.size = 2. Does anyone have a clue where it can come from?? Thank you for you help, Thierry -- 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
Sure there should be something about it because there''s 2 teams per games. But Teams belongs to League and Games belongs to 2 Teams. But since there a direct link between Team and League I didn''t expect the Games to affect the Team list (I would have expected it the other way around with 2 times each games). See below my models: class League < ActiveRecord::Base has_many :teams has_many :games, :through => :teams end class Team < ActiveRecord::Base belongs_to :club belongs_to :league has_many :games, :foreign_key => ''team_a_id'' has_many :games, :foreign_key => ''team_b_id'' end class Game < ActiveRecord::Base belongs_to :team_a, :class_name => ''Team'', :foreign_key=>''team_a_id'' belongs_to :team_b, :class_name => ''Team'', :foreign_key=>''team_b_id'' end -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Huh--interesting, those two has_many :games calls in your Team class there. Does that result in a single @my_team.games collection property that gives the complete list of games, whether they were team_a or team_b? My intuition is that the second call would sort of overwrite the first & you wouldn''t see the games from the team_a_id link. I tried to test this design out (I''m using rails 2.0.2) and that seems to be what happens. Only one of the two teams I added to a game had anything in its .games collection. I''d be tempted to do something like this instead: class Team < ActiveRecord::Base belongs_to :club belongs_to :league has_many :a_games, :class_name => ''game'', :foreign_key => ''team_a_id'' has_many :b_games, :class_name => ''game'', :foreign_key => ''team_b_id'' def games # mash the two games collections together manually. a_games + b_games end end Not that any of this addresses your problem, necessarily... -Roy -----Original Message----- From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org [mailto:rubyonrails-talk@googlegroups.com] On Behalf Of Thierry Delbart Sent: Friday, October 24, 2008 3:37 PM To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Subject: [Rails] Re: ''each'' loop 2 times trough all records Sure there should be something about it because there''s 2 teams per games. But Teams belongs to League and Games belongs to 2 Teams. But since there a direct link between Team and League I didn''t expect the Games to affect the Team list (I would have expected it the other way around with 2 times each games). See below my models: class League < ActiveRecord::Base has_many :teams has_many :games, :through => :teams end class Team < ActiveRecord::Base belongs_to :club belongs_to :league has_many :games, :foreign_key => ''team_a_id'' has_many :games, :foreign_key => ''team_b_id'' end class Game < ActiveRecord::Base belongs_to :team_a, :class_name => ''Team'', :foreign_key=>''team_a_id'' belongs_to :team_b, :class_name => ''Team'', :foreign_key=>''team_b_id'' end -- 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
Wow! Thank you for the help. I came to the same conclusions as you that just the B team games counted... I didn''t know I do this "def games" definition ... I was looking for that 3 months ago! Thank you. I used the .uniq function to clean multiple occurence of teams and games. There probably is a better way to write the code below: @games = [] listForTeams = @league.teams.uniq.dup listForTeams.each do |t| @games.concat(t.games) end @games.uniq! I I has to change part of my code to deal with team.games as a function and not an association. For example, a replaced: h[:gameplayed] = @games.find(:all, :conditions => ["team_a_id = (?) OR team_b_id = (?)", t.id, t.id]).size() by gamesplayed = @games.select { |v| v.team_a_id == t.id || v.team_b_id == t.id } h[:gamesplayed] = gamesplayed.size() it works fine, I hope I''ll have no problem going to an Ajax table with those data. Thanks again for your time. Thierry -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---