As mentioned in the title
:to keyword arguments like
So before understanding what
previously_changed? does let’s understand how
car = Car.new car.changed? # => false car.company = "Tesla" car.changed? # => true car.company_changed? # => true car.company_changed?(from: nil, to: "Tesla") # => true
As described in the API documentation
true if any of the attributes has unsaved changes,
So when we create a new
Car instance and check if it’s value has
changed? it will return
But assigning the
car object a company value
Tesla and then checking if it’s
changed? , it returns
changed? can also be used with the attributes as shown in the above example i.e.
And also with the argument
:from , to check specifically if the car’s company changed from
Tesla as shown in the example.
changed? serves the purpose well when the values are unsaved changes.
What happens if we save the changes.
car.save car.changed? # => false car.company_changed? # => false
It returns a
false value. So here is where
previously_changed? comes into play.
true if any of the attributes has saved changes,
Let’s have a look at some examples.
car.company_previously_changed? # => true car.company_previously_changed?(from: nil, to: "Tesla") # => true
We can see here that running
[attribute_name]_previously_changed? gives a true after the
company name is saved for the
And keyword argument
:to can be used in a similar fashion as in
But using this feature we have to be wary that an instance
reload may render this
car.reload car.company_previously_changed? # => false car.company_previously_changed?(from: nil, to: "Tesla") # => false