ActiveRecord hidden bug

16 Nov 2012

Today I have encountered a very weird bug.

The symptom is that a Model cannot be saved, but `model.errors` contains nothing.

It took me like an hour to figure out the real cause.

It turns out that if you do this:

class TestModel < ActiveRecord::Base belongs_to :user after_save :do_something def do_something self.user.save! # It fails here end end

If `self.user.save!` fails, then you get no error message, and your TestModel is not saved. (I think underneath it rolls back the transaction)

The solution is simple; Just populate the errors from `self.user` as well.

But if `self.user` has a similar logic (with yet another object), how should we deal with it???

Give it a kudos