I''ve got some of this working but other parts are ellusive. I have CREATE TABLE `bags` ( `id` int(11) NOT NULL auto_increment, `name` varchar(255) NOT NULL default '''', PRIMARY KEY (`id`) CREATE TABLE `packages` ( `id` int(255) NOT NULL auto_increment, `name` varchar(255) NOT NULL default '''', PRIMARY KEY (`id`) CREATE TABLE `bags_packages` ( `id` int(11) NOT NULL auto_increment, `bag_id` int(11) NOT NULL default ''0'', `package_id` int(11) NOT NULL default ''0'', PRIMARY KEY (`id`) class Package < ActiveRecord::Base has_and_belongs_to_many :bags end do I need this below? the HABTM tutorial doesn''t say I do. class Bag < ActiveRecord::Base has_many :packages end problem A: I can edit a package and select bags (using HABTM checkboxes) and bags_packages is updated with bag_id X package_id Y, but how do I get "package.bag.name" to display? Showing app/views/packages/list.rhtml where line #15 raised: undefined method `bag'' for #<Package:0xb7adc610> 15: <td><%= package.bag.name%> </td> problem B: save package "A" with bag "X" associated saves save package "B" with bag "X" associated generates error ActiveRecord::StatementInvalid in Packages#update Mysql::Error: #23000Duplicate entry ''1'' for key 1: INSERT INTO bags_packages (`id`, `bag_id`, `package_id`) VALUES (1, 1, 38) clearly the value of "1" for id is bad because it should be a newly generated ID number. any idea where this is comming from? thanks -zaq -- Posted via http://www.ruby-forum.com/.
class Bag < ActiveRecord::Base has_and_belongs_to_many :packages end -----Original Message----- From: zac elston [mailto:zelston@printingforsystems.com] Sent: Thursday, March 09, 2006 12:08 PM To: rails@lists.rubyonrails.org Subject: [Rails] habtm questions I''ve got some of this working but other parts are ellusive. I have CREATE TABLE `bags` ( `id` int(11) NOT NULL auto_increment, `name` varchar(255) NOT NULL default '''', PRIMARY KEY (`id`) CREATE TABLE `packages` ( `id` int(255) NOT NULL auto_increment, `name` varchar(255) NOT NULL default '''', PRIMARY KEY (`id`) CREATE TABLE `bags_packages` ( `id` int(11) NOT NULL auto_increment, `bag_id` int(11) NOT NULL default ''0'', `package_id` int(11) NOT NULL default ''0'', PRIMARY KEY (`id`) class Package < ActiveRecord::Base has_and_belongs_to_many :bags end do I need this below? the HABTM tutorial doesn''t say I do. class Bag < ActiveRecord::Base has_many :packages end problem A: I can edit a package and select bags (using HABTM checkboxes) and bags_packages is updated with bag_id X package_id Y, but how do I get "package.bag.name" to display? Showing app/views/packages/list.rhtml where line #15 raised: undefined method `bag'' for #<Package:0xb7adc610> 15: <td><%= package.bag.name%> </td> problem B: save package "A" with bag "X" associated saves save package "B" with bag "X" associated generates error ActiveRecord::StatementInvalid in Packages#update Mysql::Error: #23000Duplicate entry ''1'' for key 1: INSERT INTO bags_packages (`id`, `bag_id`, `package_id`) VALUES (1, 1, 38) clearly the value of "1" for id is bad because it should be a newly generated ID number. any idea where this is comming from? thanks -zaq -- Posted via http://www.ruby-forum.com/.
Buntin, Seth - KATE wrote:> class Bag < ActiveRecord::Base > has_and_belongs_to_many :packages > endI get the exact same error with or without this statement. Mysql::Error: #23000Duplicate entry ''1'' for key 1: INSERT INTO bags_packages (`id`, `bag_id`, `package_id`) VALUES (1, 1, 2) why would a bag need to belong to a package? it may have many, but certainly not belong to a package. -- Posted via http://www.ruby-forum.com/.
I might be wrong but the bags_packages table is a many-to-many relationship. This relationship is expressed in Rails by has_and_belongs_to_many. You should probably take a look at what you have set as primary keys. Have them all set as primary. You should also look at how you are creating rows. From what it looks like you are sending 1, 1, 2 twice (or more). Check your logs and see. Seth -----Original Message----- From: zac elston [mailto:zelston@printingforsystems.com] Sent: Thursday, March 09, 2006 1:03 PM To: rails@lists.rubyonrails.org Subject: [Rails] Re: RE: habtm questions Buntin, Seth - KATE wrote:> class Bag < ActiveRecord::Base > has_and_belongs_to_many :packages > endI get the exact same error with or without this statement. Mysql::Error: #23000Duplicate entry ''1'' for key 1: INSERT INTO bags_packages (`id`, `bag_id`, `package_id`) VALUES (1, 1, 2) why would a bag need to belong to a package? it may have many, but certainly not belong to a package. -- Posted via http://www.ruby-forum.com/.
There should not be an id column on the bags_packages table. zac elston wrote:> I''ve got some of this working but other parts are ellusive. > > I have > > CREATE TABLE `bags` ( > `id` int(11) NOT NULL auto_increment, > `name` varchar(255) NOT NULL default '''', > PRIMARY KEY (`id`) > > CREATE TABLE `packages` ( > `id` int(255) NOT NULL auto_increment, > `name` varchar(255) NOT NULL default '''', > PRIMARY KEY (`id`) > > CREATE TABLE `bags_packages` ( > `id` int(11) NOT NULL auto_increment, > `bag_id` int(11) NOT NULL default ''0'', > `package_id` int(11) NOT NULL default ''0'', > PRIMARY KEY (`id`) > > class Package < ActiveRecord::Base > has_and_belongs_to_many :bags > end > > do I need this below? the HABTM tutorial doesn''t say I do. > class Bag < ActiveRecord::Base > has_many :packages > end > > problem A: > I can edit a package and select bags (using HABTM checkboxes) and > bags_packages is updated with > bag_id X package_id Y, but how do I get "package.bag.name" to display? > > Showing app/views/packages/list.rhtml where line #15 raised: > undefined method `bag'' for #<Package:0xb7adc610> > 15: <td><%= package.bag.name%> </td> > > > problem B: > save package "A" with bag "X" associated saves > save package "B" with bag "X" associated generates error > > ActiveRecord::StatementInvalid in Packages#update > Mysql::Error: #23000Duplicate entry ''1'' for key 1: INSERT INTO > bags_packages (`id`, `bag_id`, `package_id`) VALUES (1, 1, 38) > > clearly the value of "1" for id is bad because it should be a newly > generated ID number. any idea where this is comming from? > > thanks > -zaq > >