hasrthur
hasrthur

Reputation: 1490

ActiveRecord detect setting of the attribute

Say, I have a model A with attribute name.

If I do this A.new(name: nil).name_changed?, it will return false.

How can I detect that the attribute was set, even if it change nothing?

Upvotes: 1

Views: 72

Answers (1)

Erez Rabih
Erez Rabih

Reputation: 15788

Try something like:

class A < ActiveRecord::Base
  attr_accessor :name_set

  def name=(v)
    @name_set = true
    super(v)
  end
end

Now you can query the object with the name_set method.

Upvotes: 3

Related Questions