rails, new release,

Swapnil Gourshete Swapnil Gourshete Follow Aug 02, 2019 · 1 min read
Share this

Rails had previous_changes method to track value of object before and after save.

  person = Person.new(name: "Joe")
  person.name = "Bill"
  person.previous_changes         # => {"name" => ["Joe", "Bill"]}
  person.name_previously_changed? # => true
  person.name_previous_change     # => ["Joe", "Bill"]
  person.reload!
  person.previous_changes         # => {}

Now *_previously_was is added to the list. It adds attribute methods for each attribute in the ActiveModel.

In our case, Person has a attribute name. So it would be

  person.name_previously_was      # => "Joe"

For a pirate class object it will look like

  pirate.update(catchphrase: "Ahoy!")
  #Before
  pirate.previous_changes["catchphrase"] # => ["Thar She Blows!", "Ahoy!"]
  #Now
  pirate.catchphrase_previously_was # => "Thar She Blows!"

SOURCE : commit

Cheers!


Swapnil Gourshete
Written by Swapnil Gourshete Follow
Hi I am Swapnil, a Software Engineer and computer science enthusiastic