Search code examples
ruby-on-railsruby-on-rails-4activerecord

ActiveRecord#save: `retry` breaks "autosave" for associations


I'm overriding save on an ActiveRecord model in order to handle an edge case:

class A < ActiveRecord::Base
  belongs_to :b

  def save *args
    super
  rescue ActiveRecord::StatementInvalid => ex
    log_warning
    retry unless @foo
    throw ex
  end
end

a = A.new
a.build_b
a.save

And I find that when the save function executes for the second time (because of the retry), I get a MySQL error pertaining to the associated record b:

ActiveRecord::StatementInvalid: Mysql2::Error: Field 'created_at' doesn't have a default value: INSERT INTO bs VALUES ()

Using a breakpoint I can see that after the retry, the invocation of ActiveRecord#_create_record (see source) for b has an empty array ([]) for the attributes to be saved, whereas it is ["full_name","company","id","owner_id","created_at","updated_at"] on the first invocation.

Why is retry breaking the auto-saving of associated records? What can I do about it?

The link above is for ActiveRecord 4.2.11.3, which is the version for the project I'm working in.


Solution

  • I wouldn't override the save method, if I were you. I would rather use the around_save callback.

    class A < ActiveRecord::Base
      around_save :saving_retry
    
      def saving_retry
        yield
      rescue ActiveRecord::StatementInvalid => ex
        log_warning
        save unless @foo
      end
    end