Guido Holz
2008-Apr-28 16:44 UTC
Both records must have an id in order to create the has_many
Hi there, my problem is to add an object to another object through the->through-statement. I tried it with an easy sample from this site: http://wiki.rubyonrails.org/rails/pages/ThroughAssociations and I got the same failure like in my own models. I built the same constelation like in the sample: class CreateCatalogueItems < ActiveRecord::Migration def self.up create_table :catalogue_items do |t| t.column :catalogue_id, :integer t.column :product_id, :integer t.timestamps end end end class Catalogue < ActiveRecord::Base has_many :catalogue_items has_many :products, :through => :catalogue_items end class Product < ActiveRecord::Base has_many :catalogue_items end and last no least the through-model class CatalogueItem < ActiveRecord::Base belongs_to :catalogue belongs_to :product end ok now I opened the console an typed in:> product = Product.new > product.save > p = Product.find(1) > catalogue = Catalogue.new > calatogue.products=> []> catalogue.products << p=> ActiveRecord::HasManyThroughCantAssociateNewRecords: Cannot associate new records through ''Catalogue#catalogue_items'' on ''#''. Both records must have an id in order to create the has_many :through record associating them. from /usr/local//lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/associations/has_many_through_association.rb:52:in `<<'' from (irb):4 I´m using rails 2.0 thanks for help Ps: Why I´m using this? transformed to my problem are products already in my system... so i seller (on my internet-platform) want to by some products... I put all the products into the catalogue and save it into a session[:catalogue] until the seller pushs te button to by it realy... I can save the catalogue but not the through-statements of the products :-( thanks for helping -- 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Apr-28 16:46 UTC
Re: Both records must have an id in order to create the has_many
On 28 Apr 2008, at 17:44, Guido Holz wrote:> > Hi there,> my problem is to add an object to another object through > the->through-statement. > >> product = Product.new >> product.save >> p = Product.find(1) >> catalogue = Catalogue.new >> calatogue.products > => [] >> catalogue.products << p > => ActiveRecord::HasManyThroughCantAssociateNewRecords: Cannot > associate > new records through ''Catalogue#catalogue_items'' on ''#''. Both records > must have an id in order to create the has_many :through record > associating them. > fromIt''s exactly was it says. You need to save catalogue first. Fred> > /usr/local//lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/ > active_record/associations/has_many_through_association.rb:52:in > `<<'' > from (irb):4 > > > I´m using rails 2.0 > > thanks for help > > Ps: Why I´m using this? transformed to my problem are products already > in my system... so i seller (on my internet-platform) want to by some > products... I put all the products into the catalogue and save it > into a > session[:catalogue] until the seller pushs te button to by it > realy... I > can save the catalogue but not the through-statements of the products > :-( > > thanks for helping > -- > 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
Guido Holz
2008-Apr-28 19:06 UTC
Re: Both records must have an id in order to create the has_
Frederick Cheung wrote:> On 28 Apr 2008, at 17:44, Guido Holz wrote: > >> >> Hi there, > >> => ActiveRecord::HasManyThroughCantAssociateNewRecords: Cannot >> associate >> new records through ''Catalogue#catalogue_items'' on ''#''. Both records >> must have an id in order to create the has_many :through record >> associating them. >> from > > It''s exactly was it says. You need to save catalogue first.I know, but what I don´t understand is: in the console I do know:> product = Product.new > product.save > arr = Array.new > arr << product > catalogue = Catalogue.new(:products => arr) > calatogue.products=> [#<Product id: 1, created_at: "2008-04-28 20:53:34", updated_at: "2008-04-28 20:53:34">] now i think without saving catalogue first, I have my first product in my catalogue-product-list but:> catalogue.save > p catalogue=> #<Catalogue id: 1, created_at: "2008-04-28 20:55:10", updated_at: "2008-04-28 20:55:10">> catalogue2 = Catalogue.find_by_id(2) > p catalogue2.products=> [] shit.... And I don´t know realy why :-( what should I do?!? take catalogue and create a knew empty catalogue2, save this and copy all attributes of catalogue into catalogue2 and save after that catalogue2 again?!? uff.... that´s heavy :-( -- 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Apr-28 19:27 UTC
Re: Both records must have an id in order to create the has_
> > arr << product > > catalogue = Catalogue.new(:products => arr)This doesn''t work. you can only specify attributes in the options to new/create.> what should I do?!? take catalogue and create a knew empty catalogue2, > save this and copy all attributes of catalogue into catalogue2 and save > after that catalogue2 again?!?Edge rails (soon to be 2.1) doesn''t have this restriction, you could always do that. If not you sill just have to save catalogue2 first before adding to catalogue2.products Fred --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Apr-28 19:35 UTC
Re: Both records must have an id in order to create the has_
On Apr 28, 8:27 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > arr << product > > > catalogue = Catalogue.new(:products => arr) > > This doesn''t work. you can only specify attributes in the options to > new/create.That may not actually be true in general.. Definitely true for has_many through though, since in 2.0 that association type has no method Fred --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---