From ebe536e3007d3617acbfebef9e6b76ff85bde982 Mon Sep 17 00:00:00 2001 From: Surender Date: Thu, 31 Mar 2016 12:00:29 +0530 Subject: [PATCH 1/2] changes, changed? and _was methods explained --- rails_tip/2016-03-31-changes_and_changed.md | 74 +++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 rails_tip/2016-03-31-changes_and_changed.md diff --git a/rails_tip/2016-03-31-changes_and_changed.md b/rails_tip/2016-03-31-changes_and_changed.md new file mode 100644 index 0000000..6b26494 --- /dev/null +++ b/rails_tip/2016-03-31-changes_and_changed.md @@ -0,0 +1,74 @@ +--- +title: Get same time of next week and previous week in rails 5 +tip-number: 13 +tip-username: Surender +tip-username-profile: https://github.com/harrysuren +tip-description: We can check the status of an ```ActiveRecord``` Object and its variables by using these methods. + +--- + + +ActiveRecord keeps a sizable amount of information about an object while it undergoes updates. A hash, accessible via the changes method, acts a central location for these updates. + +**```changes```** + +Each key in the changes hash maps to a record’s attribute. The value of each key is an Array with two elements: what the attribute value used to be, and what it is now: + +```ruby + order = Order.find(1) + order.user_id + # => 10 + order.user_id = 11 + order.changes + # => {"user_id"=>[10, 11]} +``` + +**a. ```changed?```** + +A simple boolean method changed? is available to determine if anything on the object is different since its retrieval. + +```ruby + order = Order.find(1) + order.user_id = 11 + order.changed? + # => true +``` + +However, if the same value is set on a model, regardless of the value’s object_id, changed? returns false: + +```ruby + order = Order.find(1) + order.user_id + # => 1 + order.user_id = 1 + order.changed? + # => false +``` + +Two “different” strings were set to book.title but since they resolve to the same characters, the object is not changed?. + +When using PostgreSQL, the ```changed?``` method can save unnecessary ```begin``` and ```commit``` transaction calls for objects that have no changes: + +```ruby + class OrdersController < ApplicationController + def update + @order = Order.find(order_params[:id]) + @order.assign_attributes(order_params) + @order.save! if @order.changed? + end + end +``` + +**b. ```_was```** + +Accessing the same ```changes``` hash as the ```changed?``` method, ```_was``` returns the value of an attribute before it was reassigned: + +```ruby + order = Order.find(1) + order.status + # => 'waiting' + order.status = 'delivered' + # => 'delivered' + review.status_was + # => 'waiting' +``` \ No newline at end of file From 0187818cfafdfd91cd3c8b937a12ca0b954a6b21 Mon Sep 17 00:00:00 2001 From: Harry Suren Date: Wed, 6 Apr 2016 09:48:39 +0530 Subject: [PATCH 2/2] Update 2016-03-31-changes_and_changed.md Updated the Title and Tip Number --- rails_tip/2016-03-31-changes_and_changed.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/rails_tip/2016-03-31-changes_and_changed.md b/rails_tip/2016-03-31-changes_and_changed.md index 6b26494..09d8c25 100644 --- a/rails_tip/2016-03-31-changes_and_changed.md +++ b/rails_tip/2016-03-31-changes_and_changed.md @@ -1,6 +1,6 @@ --- -title: Get same time of next week and previous week in rails 5 -tip-number: 13 +title: Check status of ActiveRecord Object +tip-number: 30 tip-username: Surender tip-username-profile: https://github.com/harrysuren tip-description: We can check the status of an ```ActiveRecord``` Object and its variables by using these methods. @@ -71,4 +71,4 @@ Accessing the same ```changes``` hash as the ```changed?``` method, ``` 'delivered' review.status_was # => 'waiting' -``` \ No newline at end of file +```