rails, new release,

Rails 6 adds *_previously_was attributes method

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.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!")
  pirate.previous_changes["catchphrase"] # => ["Thar She Blows!", "Ahoy!"]
  pirate.catchphrase_previously_was # => "Thar She Blows!"

SOURCE : commit


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