Reputation: 6827
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?
Upvotes: 1
Views: 1986
Reputation: 23450
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)
Upvotes: 2
Reputation: 176412
dolly = shepherd.animals.build(sheep.clone.attributes)
build
requires the argument to be a hash of attributes. Otherwise
dolly = shepherd.animals << sheep.clone
Upvotes: 9