Wes Gamble
2006-Jul-05 20:38 UTC
[Rails] HABTM join table has an "ID" column - is this an issue?
All, I''m building model objects for existing tables that I cannot modify. In AWDWR, Dave says "Note that our join table has no id column...The second reason for not including an id column in the join table is that AR automatically includes all columns from the join tables when accessing rows using it. If the join table included a column called id, its id would overwrite the id of the rows in the joined tables." Unfortunately, my join table does have a column called id. Is there a directive that can help me with this or some way to get around it per Dave Thomas''s quote above? Thanks, Wes -- Posted via http://www.ruby-forum.com/.
Wes Gamble
2006-Jul-05 20:45 UTC
[Rails] Re: HABTM join table has an "ID" column - is this an issue?
Wes Gamble wrote:> All, > > I''m building model objects for existing tables that I cannot modify. > > In AWDWR, Dave says "Note that our join table has no id column...The > second reason for not including an id column in the join table is that > AR automatically includes all columns from the join tables when > accessing rows using it. If the join table included a column called id, > its id would overwrite the id of the rows in the joined tables." > > Unfortunately, my join table does have a column called id. Is there a > directive that can help me with this or some way to get around it per > Dave Thomas''s quote above? > > Thanks, > WesShould I provide a value for the :select option, which will keep a SELECT * from happening on my join table? -- Posted via http://www.ruby-forum.com/.
Tom Ward
2006-Jul-05 21:17 UTC
[Rails] Re: HABTM join table has an "ID" column - is this an issue?
You could upgrade the join table to a full model and use has_many :through in place of habtm. On 7/5/06, Wes Gamble <weyus@att.net> wrote:> Wes Gamble wrote: > > All, > > > > I''m building model objects for existing tables that I cannot modify. > > > > In AWDWR, Dave says "Note that our join table has no id column...The > > second reason for not including an id column in the join table is that > > AR automatically includes all columns from the join tables when > > accessing rows using it. If the join table included a column called id, > > its id would overwrite the id of the rows in the joined tables." > > > > Unfortunately, my join table does have a column called id. Is there a > > directive that can help me with this or some way to get around it per > > Dave Thomas''s quote above? > > > > Thanks, > > Wes > > Should I provide a value for the :select option, which will keep a > SELECT * from happening on my join table? > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Wes Gamble
2006-Jul-05 21:23 UTC
[Rails] Re: Re: HABTM join table has an "ID" column - is this an iss
Tom Ward wrote:> You could upgrade the join table to a full model and use has_many > :through in place of habtm.I just realized, however, that although my join table has an "id" column, neither of the join_ed_ tables on either side of the association have "id" columns. So, I''m thinking that there shouldn''t be any collision between columns here. Where can I find out more about has_many :through? - I''m unfamiliar with this option. -- Posted via http://www.ruby-forum.com/.
Tom Ward
2006-Jul-05 21:30 UTC
[Rails] Re: Re: HABTM join table has an "ID" column - is this an iss
On 7/5/06, Wes Gamble <weyus@att.net> wrote:> Where can I find out more about has_many :through? - I''m unfamiliar with > this option.http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html#M000530 http://wiki.rubyonrails.org/rails/pages/ThroughAssociations Tom
Josh Susser
2006-Jul-06 03:08 UTC
[Rails] Re: Re: HABTM join table has an "ID" column - is this an iss
Wes Gamble wrote:> Tom Ward wrote: >> You could upgrade the join table to a full model and use has_many >> :through in place of habtm. > > I just realized, however, that although my join table has an "id" > column, neither of the join_ed_ tables on either side of the association > have "id" columns. > > So, I''m thinking that there shouldn''t be any collision between columns > here. > > Where can I find out more about has_many :through? - I''m unfamiliar with > this option.You shouldn''t have an "id" column in a join table - that will just get you in trouble. If you have anything in your join table besides the foreign_keys of the two tables being joined, you''ll have problems. In that case, you''ll want to use a join model (has_many :through) instead. http://blog.hasmanythrough.com/articles/2006/04/20/many-to-many-dance-off -- Josh Susser http://blog.hasmanythrough.com -- Posted via http://www.ruby-forum.com/.
Wes Gamble
2006-Jul-06 04:04 UTC
[Rails] Re: Re: HABTM join table has an "ID" column - is this an iss
Josh Susser wrote:> Wes Gamble wrote: >> Tom Ward wrote: >>> You could upgrade the join table to a full model and use has_many >>> :through in place of habtm. >> >> I just realized, however, that although my join table has an "id" >> column, neither of the join_ed_ tables on either side of the association >> have "id" columns. >> >> So, I''m thinking that there shouldn''t be any collision between columns >> here. >> >> Where can I find out more about has_many :through? - I''m unfamiliar with >> this option. > > You shouldn''t have an "id" column in a join table - that will just get > you in trouble. If you have anything in your join table besides the > foreign_keys of the two tables being joined, you''ll have problems. In > that case, you''ll want to use a join model (has_many :through) instead. > > http://blog.hasmanythrough.com/articles/2006/04/20/many-to-many-dance-off > > -- > Josh Susser > http://blog.hasmanythrough.comWill I have problems if _neither_ of the two tables in the HABTM relationship has an "id" column? WG -- Posted via http://www.ruby-forum.com/.
Josh Susser
2006-Jul-06 04:13 UTC
[Rails] Re: Re: HABTM join table has an "ID" column - is this an iss
Wes Gamble wrote:> Will I have problems if _neither_ of the two tables in the HABTM > relationship has an "id" column?ActiveRecord requires a primary key to manage object identity and to do joins between tables. If you have a primary key but have renamed the column from "id" to something else, you''ll be fine. If not, you''ll need to change something, or habtm won''t have anything to work with to do the joins. -- Josh Susser http://blog.hasmanythrough.com -- Posted via http://www.ruby-forum.com/.