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
:frowning:

thanks for helping

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.

Fred

Frederick C. 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 :frowning:

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 :frowning:

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

On Apr 28, 8:27 pm, Frederick C. [email protected]
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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs