Hello, I''ve searched but I can''t find a good tutorial to has and belongs to many. What should both of my models, Games and Players, look like if a game has many players and a player has many games. Also when I create I want to be able to associate the player with the game. Before I was doing @game.save for player in @home.players player.game_id = @game player.increment! :games end What should I do now to make the association? Also how do I find all players by game_id? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Doing player.game_ids = @game gives undefined method `reject'' for 1:Fixnum On Apr 7, 3:34 pm, edberner <eber...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello, I''ve searched but I can''t find a good tutorial to has and > belongs to many. > > What should both of my models, Games and Players, look like if a game > has many players and a player has many games. > > Also when I create I want to be able to associate the player with the > game. Before I was doing > @game.save > for player in @home.players > player.game_id = @game > player.increment! :games > end > > What should I do now to make the association? Also how do I find all > players by game_id?--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> Hello, I''ve searched but I can''t find a good tutorial to has and > belongs to many. > > What should both of my models, Games and Players, look like if a game > has many players and a player has many games.http://mboffin.com/stuff/ruby-on-rails-data-relationships.png> > Also when I create I want to be able to associate the player with the > game. Before I was doing > @game.save > for player in @home.players > player.game_id = @game > player.increment! :games > end > > What should I do now to make the association? Also how do I find all > players by game_id? > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Also why is increment! not working. By the way @home = Team.find_by_user_id(current_user.id) and team has many players. On Apr 7, 3:40 pm, edberner <eber...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Doing player.game_ids = @game gives > undefined method `reject'' for 1:Fixnum > > On Apr 7, 3:34 pm, edberner <eber...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hello, I''ve searched but I can''t find a good tutorial to has and > > belongs to many. > > > What should both of my models, Games and Players, look like if a game > > has many players and a player has many games. > > > Also when I create I want to be able to associate the player with the > > game. Before I was doing > > @game.save > > for player in @home.players > > player.game_id = @game > > player.increment! :games > > end > > > What should I do now to make the association? Also how do I find all > > players by game_id?--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
So if I have a Game a Player and a Team I would want to do Game has many teams (actually only 2) Team has many games all through a Player??? On Apr 7, 3:44 pm, edberner <eber...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Also why is increment! not working. > By the way @home = Team.find_by_user_id(current_user.id) > and team has many players. > > On Apr 7, 3:40 pm, edberner <eber...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Doing player.game_ids = @game gives > > undefined method `reject'' for 1:Fixnum > > > On Apr 7, 3:34 pm, edberner <eber...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Hello, I''ve searched but I can''t find a good tutorial to has and > > > belongs to many. > > > > What should both of my models, Games and Players, look like if a game > > > has many players and a player has many games. > > > > Also when I create I want to be able to associate the player with the > > > game. Before I was doing > > > @game.save > > > for player in @home.players > > > player.game_id = @game > > > player.increment! :games > > > end > > > > What should I do now to make the association? Also how do I find all > > > players by game_id?--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Apr 7, 2:48 pm, edberner <eber...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > > What should both of my models, Games and Players, look like if a game > > > > has many players and a player has many games. >The key is, a game is a connection between two teams. So a Games table would consist of three columns: id, home_team_id, away_team_id: class Game belongs_to :home_team, :class_name => "Team" belongs_to :away_team, :class_name => "Team" end class Player belongs_to :team end class Team has_many :games has_many :players end For example: cubs = Team.find(1) sox = Team.find(2) g = Game.create(:home_team => cubs, :away_team => sox) g.home_team.players # shows players on the Cubs p = cubs.players.first p.team.games # shows all the games this player is in Of course, that last bit assumes the player plays in every game the team plays in. If that''s not the case for you, you would need to add another table to tracks ''attendance'' of the player for each game. So I''ve just shown the simple case here, in the hopes it gives you a start in the right direction or is at least food for thought. Jeff softiesonrails.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 -~----------~----~----~----~------~----~------~--~---
Hi everyone. I''ve got the following in my model: class Newsletter < ActiveRecord::Base has_and_belongs_to_many :tags def tag_attributes=(tag_attributes) tag_attributes.each do |attributes| tags.build(attributes) end end end class Tag < ActiveRecord::Base has_and_belongs_to_many :newsletters end And I also got a newsletters_tags relationship table in my DB, containing newsletter_id and tag_id (both fields are set as primary_key) How do I perform a search to retrieve every tag within a particular newsletter, or all newsletters that relates to an specific tag? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Daniel Drehmer wrote:> How do I perform a search to retrieve every tag within a particular > newsletter,my_newsletter.tags> or all newsletters that relates to an specific tag?my_tag.newsletters -- 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 -~----------~----~----~----~------~----~------~--~---
@tag.newsletters or @newsletter.tags Julian. Learn Ruby on Rails! Check out the FREE VIDS (for a limited time) VIDEO #3 out NOW! http://sensei.zenunit.com/ On 10/04/2008, at 5:45 AM, Daniel Drehmer wrote:> > Hi everyone. > > I''ve got the following in my model: > > class Newsletter < ActiveRecord::Base > has_and_belongs_to_many :tags > > > def tag_attributes=(tag_attributes) > tag_attributes.each do |attributes| > tags.build(attributes) > end > end > end > > > class Tag < ActiveRecord::Base > has_and_belongs_to_many :newsletters > end > > > And I also got a newsletters_tags relationship table in my DB, > containing newsletter_id and tag_id (both fields are set as > primary_key) > > How do I perform a search to retrieve every tag within a particular > newsletter, or all newsletters that relates to an specific tag? > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---