ActiveModel::Dirty Attribute Change
Maynard Cabalitan

Maynard Cabalitan @webcoderph

About: Ruby on rails dev

Location:
Jasoom
Joined:
Apr 30, 2019

ActiveModel::Dirty Attribute Change

Publish Date: Oct 22 '19
6 0

Sometimes we need to check specific attribute if change from it previous value before running an api for request limit.

The solution is activemodel::dirty

for after save

saved_change_to_attribute?

for before save

will_save_change_to_attribute?

Example

class User
  after_save api_update, if: :saved_change_to_name?
  before_save api_update, if: :will_save_change_to_position?

  def api_update
  end
end

These callbacks will run only if the attributes change from its previous value.

Comments 0 total

    Add comment