I must have came across this previously, but i've forgotten all abt it, and today, i got bitten hard as a punishment for being forgetful. Let's same i have the following models:
1 2 3 4 5
| class Father < ActiveRecord::Base ; end
class Son < ActiveRecord::Base belongs_to :father # and yup, we have a :father_id in sons table end
|
During creating of sons, both the following strategies work:
1 2 3
| father1 = Father.create(:name => 'Papa Jones') son1 = Son.create(:father => father1) # strategy 1 son2 = Son.create(:father_id => father1.id) # strategy 2
|
But during update, only strategy 1 works:
1 2 3
| father2 = Father.create(:name => 'Papa Tommy') son1.update_attributes(:father => father2) # strategy 1 son2.update_attributes(:father_id => father2.id) # strategy 2, this DOESN'T work !!
|
DON'T FORGET !!!
No comments:
Post a Comment