Drew
Drew

Reputation: 15438

Why do I need reload objects before saving them in order for relations to update?

Not sure if this a Mongoid specific thing or if applies to the greater active record pattern. I have the following chunk of code, which works:

submission.reload.profile
submission.milestone = self.milestone
submission.profile = @profile
@profile.save && submission.save

The relation gets updated correctly. However, if I take out the first line (which I only arrived out through trial and error) it stops working and the relation does not update.

Why?

Upvotes: 1

Views: 93

Answers (1)

jjy
jjy

Reputation: 66

May be submission have some invalid attributes, when you reload, submission back to validate state and can be saved. try p submission.errors after save without reload

Upvotes: 1

Related Questions