Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update_attribute returns unexpected nil when new value is the same as current #26593

Closed
donapieppo opened this issue Sep 23, 2016 · 10 comments
Closed

Comments

@donapieppo
Copy link
Contributor

update_attribute(name, value) returns nil if the submitted value is the same as the current value.
In rails 4 it returned true (as aspected, imo).

p = Post.create(title: 'foo')
p.update_attribute(:title, 'foo') # returns nil
p.update_attribute(:title, 'bar') # return true

I think in rails 4 the db update query was always done.
In rails 5 the unecessary update is skipped but I think the return value should be true.

With update_column the update is done and the return value is always true.

Expected behavior

   p = Post.create(title: 'foo')
   p.update_attribute(:title, 'foo') # should return true

System configuration

Rails: 5.0.0.1
Ruby: 2.3.1p112

@rafaelfranca
Copy link
Member

If there is no change in the database why true is expected?
On Fri, 23 Sep 2016 at 11:15 Donapieppo notifications@github.com wrote:

update_attribute(name, value) returns nil if the submitted value is the
same as the current value.
In rails 4 it returned true (as aspected, imo).

p = Post.create(title: 'foo')
p.update_attribute(:title, 'foo') # returns nil
p.update_attribute(:title, 'bar') # return true

I think in rails 4 the db update query was always done.
In rails 5 the unecessary update is skipped but I think the return value
should be true.

With update_column the update is done and the return value is always true.
Expected behavior

p = Post.create(title: 'foo')
p.update_attribute(:title, 'foo') # should return true

System configuration

Rails: 5.0.0.1
Ruby: 2.3.1p112


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#26593, or mute the thread
https://github.com/notifications/unsubscribe-auth/AAC66GXg0fqqie-mAJKQd3CiZF48kao-ks5qs4qxgaJpZM4KEuDo
.

@donapieppo
Copy link
Contributor Author

Because I don't care what happens behind (in db) or what was the previous value.
With update_attribute(:title, 'foo') I need to know that now the current value of title is 'foo', that everything went fine. And I expect true in order to feel good an go on :-)
A nil return value is like a false return value.

@rafaelfranca
Copy link
Member

In fact the return of this method should not care for you. If you are
expecting always true, so why are you checking the return of the method?

And yes nil is false in that case and it means exactly what it means, no
attribute were updated so it return false.
On Fri, 23 Sep 2016 at 11:29 Donapieppo notifications@github.com wrote:

Because I don't care what happens behind (in db) or what was the previous
value.
With update_attribute(:title, 'foo') I need to know that now the current
value of title is 'foo', that everything went fine. And I expect true in
order to feel good an go on :-)
A nil return value is like a false return value.


You are receiving this because you commented.

Reply to this email directly, view it on GitHub
#26593 (comment), or mute
the thread
https://github.com/notifications/unsubscribe-auth/AAC66PBuz6qEsGcHaRxly2AzMqGtqgy4ks5qs43kgaJpZM4KEuDo
.

@donapieppo
Copy link
Contributor Author

I am not expecting always true. I expect true if everthing went fine and the value of title is foo.

You mean that what with update_attribute nothing can go wrong and I can never get a false result?
If that is the case maybe the doc should say that. Should say that there is no interesting return value.

If instead there can be a false (and I need to check it), than a nil return value is unexpected for an update where everything went fine.

@donapieppo
Copy link
Contributor Author

Of course if the nil return value means exactly what it means, no attribute were updated so it return false is the wanted behaviour than you can close this.

I still think that update_attribute should be idempotent (if it is the correct word) meaning that it sould return the same value when executed multiple time. It should not depend on the previous value of the attribute. As was in rails 4.

Maybe I just need to replace update_attribute with update_column.

@prathamesh-sonpatki
Copy link
Member

@rafaelfranca @donapieppo I had changed this behavior to save a SQL call in 0fcd4cf5.

I think we can return true if changed? is false to restore the old behavior. I can make a PR if this sounds like acceptable solution.

@donapieppo
Copy link
Contributor Author

a true return value with changed? false would be great for me.

Otherwise the doc should say that update_attribute returns nil if the new value is the same as the value in the database.

@rafaelfranca
Copy link
Member

👍
On Fri, 23 Sep 2016 at 12:18 Donapieppo notifications@github.com wrote:

a true return value with changed? false would be great for me.

Otherwise the doc should say that update_attribute returns nil if the new
value is the same as the value in the database.


You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub
#26593 (comment), or mute
the thread
https://github.com/notifications/unsubscribe-auth/AAC66GaalP-tzPngr6Rf70yfxq1iOYAaks5qs5lagaJpZM4KEuDo
.

@prathamesh-sonpatki
Copy link
Member

Great, I will send a PR in some time.

@prathamesh-sonpatki
Copy link
Member

r? @prathamesh-sonpatki

prathamesh-sonpatki added a commit to prathamesh-sonpatki/rails that referenced this issue Sep 23, 2016
- If the attribute is not changed, then update_attribute does not run
  SQL query, this effectively means that no change was made to the
  attribute.
- This change was made in rails@0fcd4cf5
  to avoid a SQL call.
- But the change resulted into `nil` being returned when there was no
  change in the attribute value.
- This commit corrects the behavior to return true if there is no change
  in attribute value. This is same as previous behavior of Rails 4.2
  plus benefit of no additional SQL call.
- Fixes rails#26593.
prathamesh-sonpatki added a commit that referenced this issue Sep 24, 2016
- If the attribute is not changed, then update_attribute does not run
  SQL query, this effectively means that no change was made to the
  attribute.
- This change was made in 0fcd4cf5
  to avoid a SQL call.
- But the change resulted into `nil` being returned when there was no
  change in the attribute value.
- This commit corrects the behavior to return true if there is no change
  in attribute value. This is same as previous behavior of Rails 4.2
  plus benefit of no additional SQL call.
- Fixes #26593.
prathamesh-sonpatki added a commit to prathamesh-sonpatki/rails that referenced this issue Sep 25, 2016
- If the attribute is not changed, then update_attribute does not run
  SQL query, this effectively means that no change was made to the
  attribute.
- This change was made in rails/rails@0fcd4cf
  to avoid a SQL call.
- But the change resulted into `nil` being returned when there was no
  change in the attribute value.
- This commit corrects the behavior to return true if there is no change
  in attribute value. This is same as previous behavior of Rails 4.2
  plus benefit of no additional SQL call.
- Fixes rails#26593.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants