# Both records must have an id in order to create the has\_many

**URL:** <https://www.ruby-forum.com/t/both-records-must-have-an-id-in-order-to-create-the-has-many/136077>\
**Category:** Rails\
**Created:** [April 28, 2008, 6:44pm UTC](https://www.ruby-forum.com/t/both-records-must-have-an-id-in-order-to-create-the-has-many/136077 "2008-04-28T18:44:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![der\_guido](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@der\_guido](https://www.ruby-forum.com/u/der_guido)\
**Post date:** [April 28, 2008, 6:44pm UTC](https://www.ruby-forum.com/t/both-records-must-have-an-id-in-order-to-create-the-has-many/136077/1 "2008-04-28T18:44:30Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![der\_guido](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@der\_guido](https://www.ruby-forum.com/u/der_guido)\
**Post date:** [April 28, 2008, 6:48pm UTC](https://www.ruby-forum.com/t/both-records-must-have-an-id-in-order-to-create-the-has-many/136077/2 "2008-04-28T18:48:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![der\_guido](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@der\_guido](https://www.ruby-forum.com/u/der_guido)\
**Post date:** [April 28, 2008, 9:06pm UTC](https://www.ruby-forum.com/t/both-records-must-have-an-id-in-order-to-create-the-has-many/136077/3 "2008-04-28T21:06:08Z")

</div>

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 ☹

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 ☹

---

<div class="post-metadata">

**Author:** ![der\_guido](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@der\_guido](https://www.ruby-forum.com/u/der_guido)\
**Post date:** [April 28, 2008, 9:28pm UTC](https://www.ruby-forum.com/t/both-records-must-have-an-id-in-order-to-create-the-has-many/136077/4 "2008-04-28T21:28:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![der\_guido](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@der\_guido](https://www.ruby-forum.com/u/der_guido)\
**Post date:** [April 28, 2008, 9:36pm UTC](https://www.ruby-forum.com/t/both-records-must-have-an-id-in-order-to-create-the-has-many/136077/5 "2008-04-28T21:36:53Z")

</div>

On Apr 28, 8:27 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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
