32061

Activerecord callback, previous_changes vs. changes

Question:

I'm trying to understand what the difference between these two methods is. Here is the documentation for each:

<a href="https://apidock.com/rails/v4.2.7/ActiveModel/Dirty/previous_changes" rel="nofollow">https://apidock.com/rails/v4.2.7/ActiveModel/Dirty/previous_changes</a>

<a href="https://apidock.com/rails/v4.2.7/ActiveModel/Dirty/changes" rel="nofollow">https://apidock.com/rails/v4.2.7/ActiveModel/Dirty/changes</a>

It appears to me after reading this documentation that previous_changes is what was changed after the changes are done, meaning in an after_* filter, while changes is what will be changed, meaning it's useful in a before_* filter.

Am I misunderstanding this?

Answer1:

yes you understood it rightly

These are dirty object methods

changes is used to know the changes that would take place if you try to save an object previous_changes is used to know the changes that reflected by saving an object.

But if you try to reload the object both changes and previous_changes would return empty hash {} as new copy of the record is being fetched from Database

Eg User(id: 1, name: 'Nimish', age: 24, email: 'test@example.com')

user = User.find(1) user.changes #Will output => {} user.previous_changes #Will output => {} user.name = 'Test User' user.changes #Will output => {name: ['Nimish', 'Test User']} user.previous_changes #Will output => {} user.save user.changes #Will output => {} user.previous_changes #Will output => {name: ['Nimish', 'Test User']} user.reload user.changes #Will output => {} user.previous_changes #Will output => {}

Recommend

  • I need a DBGrid OnAfterCellClick
  • LWJGL random lines inbenween shapes
  • Why does SELECT * FROM table INNER JOIN..ON show the intersecting column twice?
  • Create fake behavior for external call in development environment
  • Box2D collisions error
  • Is there a way to get a view into a python array.array()?
  • .net security exception
  • Thrust filter by key value
  • Laravel 5.1 Multitenancy setup
  • How to calculate total across columns but one?
  • Why is this button causing my layout to break?
  • Receive custom intent without activity restart
  • Not able to display correct data in table -AngularJS
  • EntLib Way to Bind “Null” Value to Parameter
  • is there a way to update filter with async data
  • Sesame : how to remove the inference during queries?
  • Doctrine/Symfony entity generator and generating entity from one table
  • How to autopopulate a field in SugarCRM form
  • Elasticsearch script query involving root and nested values
  • Differences in dis-assembled C code of GCC and Borland?
  • How to assign byte[] as a pointer in C#
  • Django invalid literal for int() with base 10
  • Installed module is empty
  • dc-js disable selecting slices on click for pie chart
  • Reading JSON from a file using C++ REST SDK (Casablanca)
  • NHibernate Validation Localization with S#arp Architecture
  • Get one-time binding to work for ng-if
  • Spring Data JPA custom method causing PropertyReferenceException
  • Sails.js/waterline: Executing waterline queries in toJSON function of a model?
  • Is there any way to access browser form field suggestions from JavaScript?
  • Spring security and special characters
  • Why ng-show works with ng-repeat but ng-if doesn't? [duplicate]
  • Excel - Autoshape get it's name from cell (value)
  • How to apply VCL Styles to DLL-based forms in Inno Setup?
  • Apache 2.4 - remove | delete | uninstall
  • retrieve vertices with no linked edge in arangodb
  • How to disable jQuery.jplayer autoplay?
  • What are the advantages and disadvantages of reading an entire file into a single String as opposed
  • Linking SubReports Without LinkChild/LinkMaster
  • Python/Django TangoWithDjango Models and Databases