For some reason, Rails keeps using the subject_id for id field in the items_subjects table. This causes a problem when I try to add a second item with the same subject as a proceeding item. I expect items_subjects to be using the auto_increment for its id field instead of manually setting it to the subject_id... Below is my code. I''m running Rails 0.14.3. Thanks, Charles require ''rubygems'' require_gem ''activerecord'' class Item < ActiveRecord::Base has_and_belongs_to_many :subjects end class Subject < ActiveRecord::Base has_and_belongs_to_many :items end class Items_Subjects < ActiveRecord::Base end Item.establish_connection( :adapter => ''mysql'', :host => ''localhost'', :username => ''root'', :password => ''password'', :database => ''retainit_test'' ) Subject.establish_connection( :adapter => ''mysql'', :host => ''localhost'', :username => ''root'', :password => ''password'', :database => ''retainit_test'' ) Items_Subjects.establish_connection( :adapter => ''mysql'', :host => ''localhost'', :username => ''root'', :password => ''password'', :database => ''retainit_test'' ) Subject.delete_all Item.delete_all Items_Subjects.delete_all Subject.create :name => "math" Subject.create :name => "addition" Subject.create :name => "subtraction" @item = Item.new @item.question = "1+1=" @item.answer = "2" @item.save @subject = Subject.find(:first, :conditions => "name = ''math''" ) @subject2 = Subject.find(:first, :conditions => "name = ''addition''" ) @item.subjects.push( [@subject, @subject2] ) @item = Item.new @item.question = "4-4=" @item.answer = "0" @item.save @subject3 = Subject.find(:first, :conditions => "name = ''math''" ) @item.subjects.push( [@subject3] ) DROP TABLE IF EXISTS `retainit_test`.`items`; CREATE TABLE `retainit_test`.`items` ( `id` int(11) NOT NULL auto_increment, `question` text, `answer` text, `difficulty` float default NULL, `stage` varchar(255) default NULL, `iteration` int(11) default NULL, `due` datetime default NULL, `lastlearned` datetime default NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; DROP TABLE IF EXISTS `retainit_test`.`items_subjects`; CREATE TABLE `retainit_test`.`items_subjects` ( `id` int(11) NOT NULL auto_increment, `subject_id` int(11) default NULL, `item_id` int(11) default NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; DROP TABLE IF EXISTS `retainit_test`.`subjects`; CREATE TABLE `retainit_test`.`subjects` ( `id` int(11) NOT NULL auto_increment, `name` varchar(255) default NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
No, that''s expected behavior. HABTM does not use a model table. If I have items id name And subjects id name HABTM will expect a table that looks like this: items_subjects ================ book_id category_id There is no ''id'' field in this table because it shouldn''t need one. You should be creating an item and then doing @item << @subject to add the subject. You shouldn''t need a model for items_subjects Also, unless you''re doing something more than what I think you''re doing, you don''t need any of this: Item.establish_connection( :adapter => ''mysql'', :host => ''localhost'', :username => ''root'', :password => ''password'', :database => ''retainit_test'' ) Subject.establish_connection( :adapter => ''mysql'', :host => ''localhost'', :username => ''root'', :password => ''password'', :database => ''retainit_test'' ) Items_Subjects.establish_connection( :adapter => ''mysql'', :host => ''localhost'', :username => ''root'', :password => ''password'', :database => ''retainit_test'' ) That''s why database.yml is for.... Hope that helps... Have you searched the WIKI for HABTM? Do you have the Agile book? Let me know if that''s clear as mud :) -----Original Message----- From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Charles Leeds Sent: Wednesday, November 09, 2005 1:30 PM To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: [Rails] HABTM - what am I doing wrong? For some reason, Rails keeps using the subject_id for id field in the items_subjects table. This causes a problem when I try to add a second item with the same subject as a proceeding item. I expect items_subjects to be using the auto_increment for its id field instead of manually setting it to the subject_id... Below is my code. I''m running Rails 0.14.3. Thanks, Charles require ''rubygems'' require_gem ''activerecord'' class Item < ActiveRecord::Base has_and_belongs_to_many :subjects end class Subject < ActiveRecord::Base has_and_belongs_to_many :items end class Items_Subjects < ActiveRecord::Base end Item.establish_connection( :adapter => ''mysql'', :host => ''localhost'', :username => ''root'', :password => ''password'', :database => ''retainit_test'' ) Subject.establish_connection( :adapter => ''mysql'', :host => ''localhost'', :username => ''root'', :password => ''password'', :database => ''retainit_test'' ) Items_Subjects.establish_connection( :adapter => ''mysql'', :host => ''localhost'', :username => ''root'', :password => ''password'', :database => ''retainit_test'' ) Subject.delete_all Item.delete_all Items_Subjects.delete_all Subject.create :name => "math" Subject.create :name => "addition" Subject.create :name => "subtraction" @item = Item.new @item.question = "1+1=" @item.answer = "2" @item.save @subject = Subject.find(:first, :conditions => "name = ''math''" ) @subject2 = Subject.find(:first, :conditions => "name = ''addition''" ) @item.subjects.push( [@subject, @subject2] ) @item = Item.new @item.question = "4-4=" @item.answer = "0" @item.save @subject3 = Subject.find(:first, :conditions => "name = ''math''" ) @item.subjects.push( [@subject3] ) DROP TABLE IF EXISTS `retainit_test`.`items`; CREATE TABLE `retainit_test`.`items` ( `id` int(11) NOT NULL auto_increment, `question` text, `answer` text, `difficulty` float default NULL, `stage` varchar(255) default NULL, `iteration` int(11) default NULL, `due` datetime default NULL, `lastlearned` datetime default NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; DROP TABLE IF EXISTS `retainit_test`.`items_subjects`; CREATE TABLE `retainit_test`.`items_subjects` ( `id` int(11) NOT NULL auto_increment, `subject_id` int(11) default NULL, `item_id` int(11) default NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; DROP TABLE IF EXISTS `retainit_test`.`subjects`; CREATE TABLE `retainit_test`.`subjects` ( `id` int(11) NOT NULL auto_increment, `name` varchar(255) default NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Thanks! Now I need to figure out how to force migrate to not create me an id field for items_subjects. Hogan, Brian P. wrote:>No, that''s expected behavior. > >HABTM does not use a model table. > >If I have > >items > id > name >And > >subjects > id > name > >HABTM will expect a table that looks like this: > >items_subjects >================> book_id > category_id > > >There is no ''id'' field in this table because it shouldn''t need one. > >You should be creating an item and then doing >@item << @subject > > to add the subject. > > >You shouldn''t need a model for items_subjects > >Also, unless you''re doing something more than what I think you''re doing, >you don''t need any of this: > >Item.establish_connection( > :adapter => ''mysql'', > :host => ''localhost'', > :username => ''root'', > :password => ''password'', > :database => ''retainit_test'' >) > >Subject.establish_connection( > :adapter => ''mysql'', > :host => ''localhost'', > :username => ''root'', > :password => ''password'', > :database => ''retainit_test'' >) > >Items_Subjects.establish_connection( > :adapter => ''mysql'', > :host => ''localhost'', > :username => ''root'', > :password => ''password'', > :database => ''retainit_test'' >) > > >That''s why database.yml is for.... > >Hope that helps... Have you searched the WIKI for HABTM? Do you have the >Agile book? > >Let me know if that''s clear as mud :) > > > >-----Original Message----- >From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >[mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Charles Leeds >Sent: Wednesday, November 09, 2005 1:30 PM >To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >Subject: [Rails] HABTM - what am I doing wrong? > > >For some reason, Rails keeps using the subject_id for id field in the >items_subjects table. This causes a problem when I try to add a second >item with the same subject as a proceeding item. I expect >items_subjects to be using the auto_increment for its id field instead >of manually setting it to the subject_id... > >Below is my code. I''m running Rails 0.14.3. > >Thanks, >Charles > >require ''rubygems'' >require_gem ''activerecord'' > >class Item < ActiveRecord::Base > has_and_belongs_to_many :subjects >end > >class Subject < ActiveRecord::Base > has_and_belongs_to_many :items >end > >class Items_Subjects < ActiveRecord::Base > >end > >Item.establish_connection( > :adapter => ''mysql'', > :host => ''localhost'', > :username => ''root'', > :password => ''password'', > :database => ''retainit_test'' >) > >Subject.establish_connection( > :adapter => ''mysql'', > :host => ''localhost'', > :username => ''root'', > :password => ''password'', > :database => ''retainit_test'' >) > >Items_Subjects.establish_connection( > :adapter => ''mysql'', > :host => ''localhost'', > :username => ''root'', > :password => ''password'', > :database => ''retainit_test'' >) > >Subject.delete_all >Item.delete_all >Items_Subjects.delete_all > >Subject.create :name => "math" >Subject.create :name => "addition" >Subject.create :name => "subtraction" > >@item = Item.new >@item.question = "1+1=" >@item.answer = "2" >@item.save > >@subject = Subject.find(:first, :conditions => "name = ''math''" ) >@subject2 = Subject.find(:first, :conditions => "name = ''addition''" ) >@item.subjects.push( [@subject, @subject2] ) > >@item = Item.new >@item.question = "4-4=" >@item.answer = "0" >@item.save > >@subject3 = Subject.find(:first, :conditions => "name = ''math''" ) >@item.subjects.push( [@subject3] ) > > >DROP TABLE IF EXISTS `retainit_test`.`items`; >CREATE TABLE `retainit_test`.`items` ( > `id` int(11) NOT NULL auto_increment, > `question` text, > `answer` text, > `difficulty` float default NULL, > `stage` varchar(255) default NULL, > `iteration` int(11) default NULL, > `due` datetime default NULL, > `lastlearned` datetime default NULL, > PRIMARY KEY (`id`) >) ENGINE=InnoDB DEFAULT CHARSET=latin1; > >DROP TABLE IF EXISTS `retainit_test`.`items_subjects`; >CREATE TABLE `retainit_test`.`items_subjects` ( > `id` int(11) NOT NULL auto_increment, > `subject_id` int(11) default NULL, > `item_id` int(11) default NULL, > PRIMARY KEY (`id`) >) ENGINE=InnoDB DEFAULT CHARSET=latin1; > >DROP TABLE IF EXISTS `retainit_test`.`subjects`; >CREATE TABLE `retainit_test`.`subjects` ( > `id` int(11) NOT NULL auto_increment, > `name` varchar(255) default NULL, > PRIMARY KEY (`id`) >) ENGINE=InnoDB DEFAULT CHARSET=latin1; >_______________________________________________ >Rails mailing list >Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ >Rails mailing list >Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >http://lists.rubyonrails.org/mailman/listinfo/rails > > > > >
Try dumping your model for items_subjects and recreate the migrations? I''m not doing anything with those yet so I won''t be much help, sorry. I thought that a migration was created from your database tables... Not sure how it works quite yet. -----Original Message----- From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Charles Leeds Sent: Wednesday, November 09, 2005 1:49 PM To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: Re: [Rails] HABTM - what am I doing wrong? Thanks! Now I need to figure out how to force migrate to not create me an id field for items_subjects. Hogan, Brian P. wrote:>No, that''s expected behavior. > >HABTM does not use a model table. > >If I have > >items > id > name >And > >subjects > id > name > >HABTM will expect a table that looks like this: > >items_subjects >================> book_id > category_id > > >There is no ''id'' field in this table because it shouldn''t need one. > >You should be creating an item and then doing >@item << @subject > > to add the subject. > > >You shouldn''t need a model for items_subjects > >Also, unless you''re doing something more than what I think you''re >doing, you don''t need any of this: > >Item.establish_connection( > :adapter => ''mysql'', > :host => ''localhost'', > :username => ''root'', > :password => ''password'', > :database => ''retainit_test'' >) > >Subject.establish_connection( > :adapter => ''mysql'', > :host => ''localhost'', > :username => ''root'', > :password => ''password'', > :database => ''retainit_test'' >) > >Items_Subjects.establish_connection( > :adapter => ''mysql'', > :host => ''localhost'', > :username => ''root'', > :password => ''password'', > :database => ''retainit_test'' >) > > >That''s why database.yml is for.... > >Hope that helps... Have you searched the WIKI for HABTM? Do you have >the Agile book? > >Let me know if that''s clear as mud :) > > > >-----Original Message----- >From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >[mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Charles Leeds >Sent: Wednesday, November 09, 2005 1:30 PM >To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >Subject: [Rails] HABTM - what am I doing wrong? > > >For some reason, Rails keeps using the subject_id for id field in the >items_subjects table. This causes a problem when I try to add a second>item with the same subject as a proceeding item. I expect >items_subjects to be using the auto_increment for its id field instead >of manually setting it to the subject_id... > >Below is my code. I''m running Rails 0.14.3. > >Thanks, >Charles > >require ''rubygems'' >require_gem ''activerecord'' > >class Item < ActiveRecord::Base > has_and_belongs_to_many :subjects >end > >class Subject < ActiveRecord::Base > has_and_belongs_to_many :items >end > >class Items_Subjects < ActiveRecord::Base > >end > >Item.establish_connection( > :adapter => ''mysql'', > :host => ''localhost'', > :username => ''root'', > :password => ''password'', > :database => ''retainit_test'' >) > >Subject.establish_connection( > :adapter => ''mysql'', > :host => ''localhost'', > :username => ''root'', > :password => ''password'', > :database => ''retainit_test'' >) > >Items_Subjects.establish_connection( > :adapter => ''mysql'', > :host => ''localhost'', > :username => ''root'', > :password => ''password'', > :database => ''retainit_test'' >) > >Subject.delete_all >Item.delete_all >Items_Subjects.delete_all > >Subject.create :name => "math" >Subject.create :name => "addition" >Subject.create :name => "subtraction" > >@item = Item.new >@item.question = "1+1=" >@item.answer = "2" >@item.save > >@subject = Subject.find(:first, :conditions => "name = ''math''" ) >@subject2 = Subject.find(:first, :conditions => "name = ''addition''" ) >@item.subjects.push( [@subject, @subject2] ) > >@item = Item.new >@item.question = "4-4=" >@item.answer = "0" >@item.save > >@subject3 = Subject.find(:first, :conditions => "name = ''math''" ) >@item.subjects.push( [@subject3] ) > > >DROP TABLE IF EXISTS `retainit_test`.`items`; >CREATE TABLE `retainit_test`.`items` ( > `id` int(11) NOT NULL auto_increment, > `question` text, > `answer` text, > `difficulty` float default NULL, > `stage` varchar(255) default NULL, > `iteration` int(11) default NULL, > `due` datetime default NULL, > `lastlearned` datetime default NULL, > PRIMARY KEY (`id`) >) ENGINE=InnoDB DEFAULT CHARSET=latin1; > >DROP TABLE IF EXISTS `retainit_test`.`items_subjects`; >CREATE TABLE `retainit_test`.`items_subjects` ( > `id` int(11) NOT NULL auto_increment, > `subject_id` int(11) default NULL, > `item_id` int(11) default NULL, > PRIMARY KEY (`id`) >) ENGINE=InnoDB DEFAULT CHARSET=latin1; > >DROP TABLE IF EXISTS `retainit_test`.`subjects`; >CREATE TABLE `retainit_test`.`subjects` ( > `id` int(11) NOT NULL auto_increment, > `name` varchar(255) default NULL, > PRIMARY KEY (`id`) >) ENGINE=InnoDB DEFAULT CHARSET=latin1; >_______________________________________________ >Rails mailing list >Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ >Rails mailing list >Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >http://lists.rubyonrails.org/mailman/listinfo/rails > > > > >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Yeah, did just that and found the answer is to set an ":id => false" into the create_table statement. It also set a :force => true, but I''m not quite sure what that does yet. create_table "items_subjects", :id => false do |t| Hogan, Brian P. wrote:>Try dumping your model for items_subjects and recreate the migrations? > >I''m not doing anything with those yet so I won''t be much help, sorry. I >thought that a migration was created from your database tables... Not >sure how it works quite yet. > >-----Original Message----- >From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >[mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Charles Leeds >Sent: Wednesday, November 09, 2005 1:49 PM >To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >Subject: Re: [Rails] HABTM - what am I doing wrong? > > >Thanks! Now I need to figure out how to force migrate to not create me >an id field for items_subjects. > >Hogan, Brian P. wrote: > > > >>No, that''s expected behavior. >> >>HABTM does not use a model table. >> >>If I have >> >>items >> id >> name >>And >> >>subjects >> id >> name >> >>HABTM will expect a table that looks like this: >> >>items_subjects >>================>> book_id >> category_id >> >> >>There is no ''id'' field in this table because it shouldn''t need one. >> >>You should be creating an item and then doing >>@item << @subject >> >>to add the subject. >> >> >>You shouldn''t need a model for items_subjects >> >>Also, unless you''re doing something more than what I think you''re >>doing, you don''t need any of this: >> >>Item.establish_connection( >> :adapter => ''mysql'', >> :host => ''localhost'', >> :username => ''root'', >> :password => ''password'', >> :database => ''retainit_test'' >>) >> >>Subject.establish_connection( >> :adapter => ''mysql'', >> :host => ''localhost'', >> :username => ''root'', >> :password => ''password'', >> :database => ''retainit_test'' >>) >> >>Items_Subjects.establish_connection( >> :adapter => ''mysql'', >> :host => ''localhost'', >> :username => ''root'', >> :password => ''password'', >> :database => ''retainit_test'' >>) >> >> >>That''s why database.yml is for.... >> >>Hope that helps... Have you searched the WIKI for HABTM? Do you have >>the Agile book? >> >>Let me know if that''s clear as mud :) >> >> >> >>-----Original Message----- >>From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>[mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Charles Leeds >>Sent: Wednesday, November 09, 2005 1:30 PM >>To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>Subject: [Rails] HABTM - what am I doing wrong? >> >> >>For some reason, Rails keeps using the subject_id for id field in the >>items_subjects table. This causes a problem when I try to add a second >> >> > > > >>item with the same subject as a proceeding item. I expect >>items_subjects to be using the auto_increment for its id field instead >>of manually setting it to the subject_id... >> >>Below is my code. I''m running Rails 0.14.3. >> >>Thanks, >>Charles >> >>require ''rubygems'' >>require_gem ''activerecord'' >> >>class Item < ActiveRecord::Base >> has_and_belongs_to_many :subjects >>end >> >>class Subject < ActiveRecord::Base >> has_and_belongs_to_many :items >>end >> >>class Items_Subjects < ActiveRecord::Base >> >>end >> >>Item.establish_connection( >> :adapter => ''mysql'', >> :host => ''localhost'', >> :username => ''root'', >> :password => ''password'', >> :database => ''retainit_test'' >>) >> >>Subject.establish_connection( >> :adapter => ''mysql'', >> :host => ''localhost'', >> :username => ''root'', >> :password => ''password'', >> :database => ''retainit_test'' >>) >> >>Items_Subjects.establish_connection( >> :adapter => ''mysql'', >> :host => ''localhost'', >> :username => ''root'', >> :password => ''password'', >> :database => ''retainit_test'' >>) >> >>Subject.delete_all >>Item.delete_all >>Items_Subjects.delete_all >> >>Subject.create :name => "math" >>Subject.create :name => "addition" >>Subject.create :name => "subtraction" >> >>@item = Item.new >>@item.question = "1+1=" >>@item.answer = "2" >>@item.save >> >>@subject = Subject.find(:first, :conditions => "name = ''math''" ) >>@subject2 = Subject.find(:first, :conditions => "name = ''addition''" ) >>@item.subjects.push( [@subject, @subject2] ) >> >>@item = Item.new >>@item.question = "4-4=" >>@item.answer = "0" >>@item.save >> >>@subject3 = Subject.find(:first, :conditions => "name = ''math''" ) >>@item.subjects.push( [@subject3] ) >> >> >>DROP TABLE IF EXISTS `retainit_test`.`items`; >>CREATE TABLE `retainit_test`.`items` ( >> `id` int(11) NOT NULL auto_increment, >> `question` text, >> `answer` text, >> `difficulty` float default NULL, >> `stage` varchar(255) default NULL, >> `iteration` int(11) default NULL, >> `due` datetime default NULL, >> `lastlearned` datetime default NULL, >> PRIMARY KEY (`id`) >>) ENGINE=InnoDB DEFAULT CHARSET=latin1; >> >>DROP TABLE IF EXISTS `retainit_test`.`items_subjects`; >>CREATE TABLE `retainit_test`.`items_subjects` ( >> `id` int(11) NOT NULL auto_increment, >> `subject_id` int(11) default NULL, >> `item_id` int(11) default NULL, >> PRIMARY KEY (`id`) >>) ENGINE=InnoDB DEFAULT CHARSET=latin1; >> >>DROP TABLE IF EXISTS `retainit_test`.`subjects`; >>CREATE TABLE `retainit_test`.`subjects` ( >> `id` int(11) NOT NULL auto_increment, >> `name` varchar(255) default NULL, >> PRIMARY KEY (`id`) >>) ENGINE=InnoDB DEFAULT CHARSET=latin1; >>_______________________________________________ >>Rails mailing list >>Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>http://lists.rubyonrails.org/mailman/listinfo/rails >>_______________________________________________ >>Rails mailing list >>Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>http://lists.rubyonrails.org/mailman/listinfo/rails >> >> >> >> >> >> >> > >_______________________________________________ >Rails mailing list >Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ >Rails mailing list >Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >http://lists.rubyonrails.org/mailman/listinfo/rails > > > > >
The :force => true drops the table before the create table is done to ensures that the table can be created, consequently, by force. - Nathan> -----Original Message----- > From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of > Charles Leeds > Sent: November 9, 2005 12:01 PM > To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > Subject: Re: [Rails] HABTM - what am I doing wrong? > > Yeah, did just that and found the answer is to set an ":id => false" > into the create_table statement. It also set a :force => > true, but I''m not quite sure what that does yet. > > create_table "items_subjects", :id => false do |t| > > Hogan, Brian P. wrote: > > >Try dumping your model for items_subjects and recreate the > migrations? > > > >I''m not doing anything with those yet so I won''t be much > help, sorry. I > >thought that a migration was created from your database > tables... Not > >sure how it works quite yet. > > > >-----Original Message----- > >From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > >[mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of > Charles Leeds > >Sent: Wednesday, November 09, 2005 1:49 PM > >To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > >Subject: Re: [Rails] HABTM - what am I doing wrong? > > > > > >Thanks! Now I need to figure out how to force migrate to > not create me > >an id field for items_subjects. > > > >Hogan, Brian P. wrote: > > > > > > > >>No, that''s expected behavior. > >> > >>HABTM does not use a model table. > >> > >>If I have > >> > >>items > >> id > >> name > >>And > >> > >>subjects > >> id > >> name > >> > >>HABTM will expect a table that looks like this: > >> > >>items_subjects > >>================> >> book_id > >> category_id > >> > >> > >>There is no ''id'' field in this table because it shouldn''t need one. > >> > >>You should be creating an item and then doing @item << @subject > >> > >>to add the subject. > >> > >> > >>You shouldn''t need a model for items_subjects > >> > >>Also, unless you''re doing something more than what I think you''re > >>doing, you don''t need any of this: > >> > >>Item.establish_connection( > >> :adapter => ''mysql'', > >> :host => ''localhost'', > >> :username => ''root'', > >> :password => ''password'', > >> :database => ''retainit_test'' > >>) > >> > >>Subject.establish_connection( > >> :adapter => ''mysql'', > >> :host => ''localhost'', > >> :username => ''root'', > >> :password => ''password'', > >> :database => ''retainit_test'' > >>) > >> > >>Items_Subjects.establish_connection( > >> :adapter => ''mysql'', > >> :host => ''localhost'', > >> :username => ''root'', > >> :password => ''password'', > >> :database => ''retainit_test'' > >>) > >> > >> > >>That''s why database.yml is for.... > >> > >>Hope that helps... Have you searched the WIKI for HABTM? Do > you have > >>the Agile book? > >> > >>Let me know if that''s clear as mud :) > >> > >> > >> > >>-----Original Message----- > >>From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > >>[mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Charles > >>Leeds > >>Sent: Wednesday, November 09, 2005 1:30 PM > >>To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > >>Subject: [Rails] HABTM - what am I doing wrong? > >> > >> > >>For some reason, Rails keeps using the subject_id for id > field in the > >>items_subjects table. This causes a problem when I try to add a > >>second > >> > >> > > > > > > > >>item with the same subject as a proceeding item. I expect > >>items_subjects to be using the auto_increment for its id > field instead > >>of manually setting it to the subject_id... > >> > >>Below is my code. I''m running Rails 0.14.3. > >> > >>Thanks, > >>Charles > >> > >>require ''rubygems'' > >>require_gem ''activerecord'' > >> > >>class Item < ActiveRecord::Base > >> has_and_belongs_to_many :subjects > >>end > >> > >>class Subject < ActiveRecord::Base > >> has_and_belongs_to_many :items > >>end > >> > >>class Items_Subjects < ActiveRecord::Base > >> > >>end > >> > >>Item.establish_connection( > >> :adapter => ''mysql'', > >> :host => ''localhost'', > >> :username => ''root'', > >> :password => ''password'', > >> :database => ''retainit_test'' > >>) > >> > >>Subject.establish_connection( > >> :adapter => ''mysql'', > >> :host => ''localhost'', > >> :username => ''root'', > >> :password => ''password'', > >> :database => ''retainit_test'' > >>) > >> > >>Items_Subjects.establish_connection( > >> :adapter => ''mysql'', > >> :host => ''localhost'', > >> :username => ''root'', > >> :password => ''password'', > >> :database => ''retainit_test'' > >>) > >> > >>Subject.delete_all > >>Item.delete_all > >>Items_Subjects.delete_all > >> > >>Subject.create :name => "math" > >>Subject.create :name => "addition" > >>Subject.create :name => "subtraction" > >> > >>@item = Item.new > >>@item.question = "1+1=" > >>@item.answer = "2" > >>@item.save > >> > >>@subject = Subject.find(:first, :conditions => "name = ''math''" ) > >>@subject2 = Subject.find(:first, :conditions => "name = > ''addition''" ) > >>@item.subjects.push( [@subject, @subject2] ) > >> > >>@item = Item.new > >>@item.question = "4-4=" > >>@item.answer = "0" > >>@item.save > >> > >>@subject3 = Subject.find(:first, :conditions => "name = ''math''" ) > >>@item.subjects.push( [@subject3] ) > >> > >> > >>DROP TABLE IF EXISTS `retainit_test`.`items`; CREATE TABLE > >>`retainit_test`.`items` ( `id` int(11) NOT NULL auto_increment, > >>`question` text, `answer` text, `difficulty` float default NULL, > >>`stage` varchar(255) default NULL, `iteration` int(11) > default NULL, > >>`due` datetime default NULL, `lastlearned` datetime default NULL, > >>PRIMARY KEY (`id`) > >>) ENGINE=InnoDB DEFAULT CHARSET=latin1; > >> > >>DROP TABLE IF EXISTS `retainit_test`.`items_subjects`; > CREATE TABLE > >>`retainit_test`.`items_subjects` ( `id` int(11) NOT NULL > >>auto_increment, `subject_id` int(11) default NULL, > `item_id` int(11) > >>default NULL, PRIMARY KEY (`id`) > >>) ENGINE=InnoDB DEFAULT CHARSET=latin1; > >> > >>DROP TABLE IF EXISTS `retainit_test`.`subjects`; CREATE TABLE > >>`retainit_test`.`subjects` ( `id` int(11) NOT NULL > auto_increment, > >>`name` varchar(255) default NULL, PRIMARY KEY (`id`) > >>) ENGINE=InnoDB DEFAULT CHARSET=latin1; > >>_______________________________________________ > >>Rails mailing list > >>Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > >>http://lists.rubyonrails.org/mailman/listinfo/rails > >>_______________________________________________ > >>Rails mailing list > >>Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > >>http://lists.rubyonrails.org/mailman/listinfo/rails > >> > >> > >> > >> > >> > >> > >> > > > >_______________________________________________ > >Rails mailing list > >Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > >http://lists.rubyonrails.org/mailman/listinfo/rails > >_______________________________________________ > >Rails mailing list > >Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > >http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >