I’m not able to update a record that has an association in Rails.
For example, I have a Post model and a User model. In post.rb I’ve included the association belongs_to :user.
If I want to change the user_id field for an existing Post record, it doesn’t work.
p = Post.find(1)
p.user_id = 5
p.save
The above doesn’t change the Post record’s user_id field to 5. When I remove the association, the above code works.
Is there a way to update the user_id field without removing the association?
Thanks!
Tim
You wrote:
You can easily verify if it’s due to validations by replacing
"save"with"save!"The former just returns false if any of the validations fail, whereas the latter will raise an exception:
Alternatively, you can also do the following in a Rails console:
Good luck!