Frank Kim
2010-Mar-02 05:47 UTC
how I do different has_and_belongs_to_many for the same model?
Hi,
I have a team which has and belongs to many players.
class Team < ActiveRecord::Base
has_and_belongs_to_many :players
end
class Player < ActiveRecord::Base
has_and_belongs_to_many :teams
end
What I''d like to do is add bench players to my team. The bench will
have many players and many players can be on the bench.
I tried to do it like this:
class Team < ActiveRecord::Base
has_and_belongs_to_many :players
has_and_belongs_to_many :bench_players, :class =>
''Player''
end
I made the association table like this:
class TeamsBenchPlayers < ActiveRecord::Migration
def self.up
create_table :teams_bench_players, :id => false do |t|
t.column :team_id, :integer, :null => false
t.column :player_id, :integer, :null => false
end
add_index :teams_bench_players, [:team_id]
add_index :teams_bench_players, [:player_id]
end
def self.down
drop_table :teams_bench_players
end
end
However when I do this:
team.bench_players << player
It saves it in the wrong table, i.e. the team_players table. What am
I doing wrong?
Thanks,
Frank
--
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.
Frederick Cheung
2010-Mar-02 11:00 UTC
Re: how I do different has_and_belongs_to_many for the same model?
On Mar 2, 5:47 am, Frank Kim <railso...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> However when I do this: > > team.bench_players << player > > It saves it in the wrong table, i.e. the team_players table. What am > I doing wrong?You need to tell activerecord to use a different join table (with the :join_table) option Fred> > Thanks, > Frank-- 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.
Ar Chron
2010-Mar-02 13:58 UTC
Re: how I do different has_and_belongs_to_many for the same model?
Some might argue that being on the bench or not is simply an attribute of a player... benched or playing, the player is still on the team. -- 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.
Frank Kim
2010-Mar-02 14:01 UTC
Re: Re: how I do different has_and_belongs_to_many for the same model?
Thanks Frederick, I forgot about that option. On Tue, Mar 2, 2010 at 3:00 AM, Frederick Cheung <frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > On Mar 2, 5:47 am, Frank Kim <railso...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> However when I do this: >> >> team.bench_players << player >> >> It saves it in the wrong table, i.e. the team_players table. What am >> I doing wrong? > > You need to tell activerecord to use a different join table (with > the :join_table) option > > Fred >> >> Thanks, >> Frank > > -- > 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. > >-- Frank Kim http://betweengo.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.
Frank Kim
2010-Mar-02 14:04 UTC
Re: Re: how I do different has_and_belongs_to_many for the same model?
Hi Ar, The attribute idea makes a lot of sense. If I did it that way then I could use the same join table. However I would have to use specify custom SQL for how to select from it, right? Or is there a better way? -Frank On Tue, Mar 2, 2010 at 5:58 AM, Ar Chron <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Some might argue that being on the bench or not is simply an attribute > of a player... benched or playing, the player is still on the team. > -- > 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. > >-- Frank Kim http://betweengo.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.
Aldric Giacomoni
2010-Mar-02 14:22 UTC
Re: Re: how I do different has_and_belongs_to_many for the same model?
Frank Kim wrote:> Hi Ar, > The attribute idea makes a lot of sense. If I did it that way then I > could use the same join table. However I would have to use specify > custom SQL for how to select from it, right? Or is there a better > way?You could just have "benched" be a boolean field in the database. If it''s true, the player''s on the bench. If it''s false, he''s on the field. Then you can do this: player = Player.find_by_last_name "Ronaldo" player.benched = true # Be a team player, Ronaldo! -- 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.
Matt Jones
2010-Mar-02 18:51 UTC
Re: how I do different has_and_belongs_to_many for the same model?
Not sure if has_and_belongs_to_many is exactly right (can a player be
on more than one team?) but one option is to add :conditions to the
declarations:
has_and_belongs_to_many :benched_players, :join_table =>
''teams_players'', :association_foreign_key =>
''player_id'', :class_name
=> ''Player'', :conditions => { :benched => true }
(not sure if this is 100% of what you need, but you get the idea)
On the other hand, if a player really can only be on one team, you''ll
have a much better time if you switch back to a plain has_many. On the
other hand, if you''ve got a situation where players *can* be on
multiple teams, it''s possible that they could be "benched"
only on
some of them; in that case, you''ll want a "decorated join
model", with
has_many :through.
--Matt Jones
On Mar 2, 9:04 am, Frank Kim
<railso...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> Hi Ar,
> The attribute idea makes a lot of sense. If I did it that way then I
> could use the same join table. However I would have to use specify
> custom SQL for how to select from it, right? Or is there a better
> way?
> -Frank
--
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.
Charles A. Lopez
2010-Mar-02 19:22 UTC
Re: Re: how I do different has_and_belongs_to_many for the same model?
The attribute (Matt Jones calls it a "condition") of being on the bench or not on the bench is a property of the player not the team. A team does not belong to the players. Players make up the team. Consider the deletion anomaly. If a Player is removed, does the team cease to exist? However, If a team is removed, then none of the players can theoretically belong to that team. On 2 March 2010 13:51, Matt Jones <al2o3cr-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Not sure if has_and_belongs_to_many is exactly right (can a player be > on more than one team?) but one option is to add :conditions to the > declarations: > > has_and_belongs_to_many :benched_players, :join_table => > ''teams_players'', :association_foreign_key => ''player_id'', :class_name > => ''Player'', :conditions => { :benched => true } > > (not sure if this is 100% of what you need, but you get the idea) > > On the other hand, if a player really can only be on one team, you''ll > have a much better time if you switch back to a plain has_many. On the > other hand, if you''ve got a situation where players *can* be on > multiple teams, it''s possible that they could be "benched" only on > some of them; in that case, you''ll want a "decorated join model", with > has_many :through. > > --Matt Jones > > > On Mar 2, 9:04 am, Frank Kim <railso...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Hi Ar, > > The attribute idea makes a lot of sense. If I did it that way then I > > could use the same join table. However I would have to use specify > > custom SQL for how to select from it, right? Or is there a better > > way? > > -Frank > > -- > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- Charles A. Lopez charlesalopez-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org What''s your vision for your organization? What''s your biggest challenge? Let''s talk. (IBM Partner) -- 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.
Frank Kim
2010-Mar-02 22:29 UTC
Re: Re: Re: how I do different has_and_belongs_to_many for the same model?
That makes a lot of sense Ar. Thanks! On Tue, Mar 2, 2010 at 6:22 AM, Aldric Giacomoni <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Frank Kim wrote: >> Hi Ar, >> The attribute idea makes a lot of sense. If I did it that way then I >> could use the same join table. However I would have to use specify >> custom SQL for how to select from it, right? Or is there a better >> way? > > You could just have "benched" be a boolean field in the database. If > it''s true, the player''s on the bench. If it''s false, he''s on the field. > Then you can do this: > > player = Player.find_by_last_name "Ronaldo" > player.benched = true # Be a team player, Ronaldo! > -- > 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. > >-- Frank Kim http://betweengo.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.
Frank Kim
2010-Mar-02 23:10 UTC
Re: Re: Re: how I do different has_and_belongs_to_many for the same model?
Hi Ar, Actually I realized this won''t work because players can be on multiple teams and can be benched on one team but not the other. Therefore it can''t be an attribute of the player, it has to be an attribute of the team. Looks like I''ll have to go back to my old idea. -Frank On Tue, Mar 2, 2010 at 6:22 AM, Aldric Giacomoni <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Frank Kim wrote: >> Hi Ar, >> The attribute idea makes a lot of sense. If I did it that way then I >> could use the same join table. However I would have to use specify >> custom SQL for how to select from it, right? Or is there a better >> way? > > You could just have "benched" be a boolean field in the database. If > it''s true, the player''s on the bench. If it''s false, he''s on the field. > Then you can do this: > > player = Player.find_by_last_name "Ronaldo" > player.benched = true # Be a team player, Ronaldo! > -- > 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. > >-- Frank Kim http://betweengo.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.
Frank Kim
2010-Mar-02 23:53 UTC
Re: Re: how I do different has_and_belongs_to_many for the same model?
Frederick, That was the solution. Thanks! -Frank On Tue, Mar 2, 2010 at 6:01 AM, Frank Kim <railsonly-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Thanks Frederick, I forgot about that option. > > On Tue, Mar 2, 2010 at 3:00 AM, Frederick Cheung > <frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> >> >> On Mar 2, 5:47 am, Frank Kim <railso...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> >>> However when I do this: >>> >>> team.bench_players << player >>> >>> It saves it in the wrong table, i.e. the team_players table. What am >>> I doing wrong? >> >> You need to tell activerecord to use a different join table (with >> the :join_table) option >> >> Fred >>> >>> Thanks, >>> Frank >> >> -- >> 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. >> >> > > > > -- > Frank Kim > http://betweengo.com/ >-- Frank Kim http://betweengo.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.