views:

352

answers:

2

shepherd has_many animals. I am trying to clone one of them:

dolly=shepherd.animals.build(sheep.clone)

I get error:

undefined method `stringify_keys!' for #<Sheep:0xb6ce154c>

why? what is another way to clone dolly so that she would be associated with a shepherd and have sheep's attributes?

+5  A: 
dolly = shepherd.animals.build(sheep.clone.attributes)

build requires the argument to be a hash of attributes. Otherwise

dolly = shepherd.animals << sheep.clone
Simone Carletti
+2  A: 

ActiveRecord::Base constructors take a parameter hash. Passing an object doesn't quite do it. So you need to query the attributes hash of the object in question.

dolly=shepherd.animals.build(sheep.clone.attributes)

In fact the constructors ignore the id attribute, so you can get away with:

dolly=shepherd.animals.build(sheep.attributes)
EmFi