Hi all, I have a couple of classes associated by a has_and_belongs_to many relationship: class Collection < ActiveRecord::Base belongs_to :owner, :class_name => "User", :foreign_key => "owner_id" has_and_belongs_to_many :books ... end class Book < ActiveRecord::Base has_and_belongs_to_many :collections ... end In the Collection_controller index method, I recover some collections and call the list view: def index @collections = Collection.find_all_by_owner_id(@session[''user''].id) render_action ''list'' end The view is as follows: <ul> <% for collection in @collections %> <li> <%=h collection.name %>(<%= collection.books.size %>) <%= link_to ''Show'', :action => ''show'', :id => collection.id %> <%= link_to ''Edit'', :action => ''edit'', :id => collection.id %> <%= link_to ''Destroy'', :action => ''destroy'', :id => collection.id %> <ol> <% for book in collection.books %> <li><%= link_to_image(book.cover_thumb, {:controller=>"book", :id => book.id , :action => "show" }) %><br/>[<%= book.ISBN %>]<%=h book.title%> </li> <%end%> </ol> </li> <% end %> </ul> However, all the link that get built have the same url: http://127.0.0.1:3000/book/show/0. That is, the id for all associated books appears as 0. If I set the view to show those values, they are effectively 0. However, all other attributes are perfectly valid. What am I doing wrong? TIA cheers victor
Hi again, I solved it after staring the db in the eyes for a long time. It turns out I had an unnecesary column id in the intermediate join table. Let the list archive this message for reference, it might eventually come handy. cheers Victor Victor Jalencas wrote:> Hi all, > > I have a couple of classes associated by a has_and_belongs_to many > relationship: > > > class Collection < ActiveRecord::Base > > belongs_to :owner, :class_name => "User", :foreign_key => "owner_id" > has_and_belongs_to_many :books > ... > end > > > class Book < ActiveRecord::Base > has_and_belongs_to_many :collections > ... > end > > > In the Collection_controller index method, I recover some collections > and call the list view: > > def index > @collections = Collection.find_all_by_owner_id(@session[''user''].id) > render_action ''list'' > end > > The view is as follows: > <ul> > > > <% for collection in @collections %> > <li> > <%=h collection.name %>(<%= collection.books.size %>) <%= link_to > ''Show'', :action => ''show'', :id => collection.id %> <%= link_to ''Edit'', > :action => ''edit'', :id => collection.id %> <%= link_to ''Destroy'', > :action => ''destroy'', :id => collection.id %> > <ol> > <% for book in collection.books %> > > <li><%= link_to_image(book.cover_thumb, {:controller=>"book", :id > => book.id , :action => "show" }) %><br/>[<%= book.ISBN > %>]<%=h book.title%> </li> > <%end%> > </ol> > </li> > <% end %> > </ul> > > However, all the link that get built have the same url: > http://127.0.0.1:3000/book/show/0. That is, the id for all associated > books appears as 0. If I set the view to show those values, they are > effectively 0. However, all other attributes are perfectly valid. What > am I doing wrong? > > TIA > > cheers > > victor > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Hi Victor, On Sun, 23 Jan 2005 21:27:24 +0100, Victor Jalencas <victor-ronr-ZNEtucZYht7k1uMJSBkQmQ@public.gmane.org> wrote:> However, all the link that get built have the same url: > http://127.0.0.1:3000/book/show/0. That is, the id for all associated > books appears as 0. If I set the view to show those values, they are > effectively 0. However, all other attributes are perfectly valid. What > am I doing wrong?Do you have an "id" field in your has_and_belongs_to_many join table? If so, remove it. Sam
On Jan 25, 2005, at 9:17, Sam Stephenson wrote:> On Sun, 23 Jan 2005 21:27:24 +0100, Victor Jalencas wrote: >> However, all the link that get built have the same url: >> http://127.0.0.1:3000/book/show/0. That is, the id for all associated >> books appears as 0. If I set the view to show those values, they are >> effectively 0. However, all other attributes are perfectly valid. What >> am I doing wrong? > > Do you have an "id" field in your has_and_belongs_to_many join table? > If so, remove it.So the join table does not need a primary key? Thijs -- Fingertips - http://www.fngtps.com Firefox - Take back the web - http://getfirefox.com
This was somewhat confusing to me at first as well. I believe the assumption made here is that there will never be an association between two objects more than once. For example: Person has_and_belongs_to_many Phone Numbers as in: Jim''s phone number is 123-4567 Jim''s cell phone number is 789-1234 Jane''s phone number is 123-4567 There should never be a case when a phone number is associated with the same person twice. I have found cases, however, when this assumption is not necessarily true, and so it would be nice if the has_and_belongs_to_many assocation could be extended to allow for an id field as well. (The case that I needed such an association was when I had a trucking transport database that shipped vehicles from place to place. Each "booking" had many "vehicles", and each "vehicle" could appear on one or several "bookings". However, it is not true that there can not be two of the same vehicle on one booking.) I wonder if there is a solution in the works? Duane Johnson On Tue, 25 Jan 2005 17:54:13 +0100, Thijs van der Vossen <t.vandervossen-d8uFP1XuNEbQT0dZR+AlfA@public.gmane.org> wrote:> On Jan 25, 2005, at 9:17, Sam Stephenson wrote: > > On Sun, 23 Jan 2005 21:27:24 +0100, Victor Jalencas wrote: > >> However, all the link that get built have the same url: > >> http://127.0.0.1:3000/book/show/0. That is, the id for all associated > >> books appears as 0. If I set the view to show those values, they are > >> effectively 0. However, all other attributes are perfectly valid. What > >> am I doing wrong? > > > > Do you have an "id" field in your has_and_belongs_to_many join table? > > If so, remove it. > > So the join table does not need a primary key? > > Thijs > > -- > Fingertips - http://www.fngtps.com > Firefox - Take back the web - http://getfirefox.com > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
* Duane Johnson (duane.johnson-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org) [050125 14:57]:> This was somewhat confusing to me at first as well. I believe the > assumption made here is that there will never be an association > between two objects more than once. For example: > > Person has_and_belongs_to_many Phone Numbers as in: > > Jim''s phone number is 123-4567 > Jim''s cell phone number is 789-1234 > Jane''s phone number is 123-4567 > > There should never be a case when a phone number is associated with > the same person twice.I think the problem is that use of has_and_belongs_to_many may not be appropriate for this particular data model. Really there''s a cell phone habtm relationship and a telephone habtm relationship, since it sounds like there''s a difference in this application (truthfully, I''d most likely just store the #s within the person record, or factor out all contact information to a contact record and associate that with a person). There are a lot of ways to skin this simple cat (and as a representative of a more complex setup it''s still worth considering in some detail), but it appears that you have more than one habtm relationship here (meaning there are multiple association tables). There''s nothing particularly bad about this, and Rails supports this, you just need to specify the relationships for AR to get the picture. Rick -- http://www.rickbradley.com MUPRN: 278 | this is possible, random email haiku | feasible, or would even | help solve the problem...
Duane, On 25.1.2005, at 21:56, Duane Johnson wrote:> I have found cases, however, when this assumption is not necessarily > true, and so it would be nice if the has_and_belongs_to_many > assocation could be extended to allow for an id field as well. (The > case that I needed such an association was when I had a trucking > transport database that shipped vehicles from place to place. Each > "booking" had many "vehicles", and each "vehicle" could appear on one > or several "bookings". However, it is not true that there can not be > two of the same vehicle on one booking.)I don''t think you''re talking about a true many-to-many relationship here. What I''d do is to create another model, booking_row, which has one-to-many relationships with both bookings and vehicles: def BookingRow < ActiveRecord::Base belongs_to :booking belongs_to :vehicle end This way you will get an id for every row. //jarkko> > I wonder if there is a solution in the works? > > Duane Johnson-- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails