[Rails-core] update_attribute violates POLA
POLA http://en.wikipedia.org/wiki/Principle_of_least_astonishment Examples ``` # user.rb class User ActiveRecord::Base attr_accessible :name, :username validates :username, uniqueness: true end user1 = User.create(name: 'Name 1', username: 'username1') user2 = User.create(name: 'Name 2', username: 'username2') user1.username = 'username2' user1.save # = false user1.update_attribute(name: 'New Name') user1.reload user1.username # = 'username2' # update_attribute is expected to update the specified attribute, not other ones. Thus it is violating POLA ``` -- You received this message because you are subscribed to the Google Groups Ruby on Rails: Core group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-core+unsubscr...@googlegroups.com. To post to this group, send email to rubyonrails-core@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core. For more options, visit https://groups.google.com/d/optout.
Re: [Rails-core] update_attribute violates POLA
On Mar 12, 2014, at 7:23 AM, Anh Nguyen khac...@gmail.com wrote: POLA http://en.wikipedia.org/wiki/Principle_of_least_astonishment Examples ``` # user.rb class User ActiveRecord::Base attr_accessible :name, :username validates :username, uniqueness: true end user1 = User.create(name: 'Name 1', username: 'username1') user2 = User.create(name: 'Name 2', username: 'username2') user1.username = 'username2' user1.save # = false user1.update_attribute(name: 'New Name') user1.reload user1.username # = 'username2' # update_attribute is expected to update the specified attribute, not other ones. Thus it is violating POLA ``` This behavior was exactly why update_attribute was (briefly) deprecated in 3.2.7: https://groups.google.com/forum/?hl=enfromgroups#!topic/rubyonrails-core/BWPUTK7WvYA Not sure how it got un-deprecated... --Matt Jones -- You received this message because you are subscribed to the Google Groups Ruby on Rails: Core group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-core+unsubscr...@googlegroups.com. To post to this group, send email to rubyonrails-core@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core. For more options, visit https://groups.google.com/d/optout.
Re: [Rails-core] update_attribute violates POLA
On 12-03-2014 08:23, Anh Nguyen wrote: POLA http://en.wikipedia.org/wiki/Principle_of_least_astonishment Examples ``` # user.rb class User ActiveRecord::Base attr_accessible :name, :username validates :username, uniqueness: true end user1 = User.create(name: 'Name 1', username: 'username1') user2 = User.create(name: 'Name 2', username: 'username2') user1.username = 'username2' user1.save # = false user1.update_attribute(name: 'New Name') user1.reload user1.username # = 'username2' # update_attribute is expected to update the specified attribute, not other ones. Thus it is violating POLA ``` I don't understand. What were you expecting? You have updated the name, not the username. -- You received this message because you are subscribed to the Google Groups Ruby on Rails: Core group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-core+unsubscr...@googlegroups.com. To post to this group, send email to rubyonrails-core@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core. For more options, visit https://groups.google.com/d/optout.
Re: [Rails-core] update_attribute violates POLA
The issue here does not seem to be what everyone is addressing. If I understood Anh correctly, when you run update_attribute X on a unsaved model that has one attribute Y set in memory, calling reload will report both X and Y as saved, so it seems to be a reload issue at first glance. I will try and reproduce it shortly. On Wed, Mar 12, 2014 at 3:13 PM, Rodrigo Rosenfeld Rosas rr.ro...@gmail.com wrote: On 12-03-2014 08:23, Anh Nguyen wrote: POLA http://en.wikipedia.org/wiki/Principle_of_least_astonishment Examples ``` # user.rb class User ActiveRecord::Base attr_accessible :name, :username validates :username, uniqueness: true end user1 = User.create(name: 'Name 1', username: 'username1') user2 = User.create(name: 'Name 2', username: 'username2') user1.username = 'username2' user1.save # = false user1.update_attribute(name: 'New Name') user1.reload user1.username # = 'username2' # update_attribute is expected to update the specified attribute, not other ones. Thus it is violating POLA ``` I don't understand. What were you expecting? You have updated the name, not the username. -- You received this message because you are subscribed to the Google Groups Ruby on Rails: Core group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-core+unsubscr...@googlegroups.com. To post to this group, send email to rubyonrails-core@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core. For more options, visit https://groups.google.com/d/optout. -- Mohamed Wael Khobalatte -- You received this message because you are subscribed to the Google Groups Ruby on Rails: Core group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-core+unsubscr...@googlegroups.com. To post to this group, send email to rubyonrails-core@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core. For more options, visit https://groups.google.com/d/optout.
Re: [Rails-core] update_attribute violates POLA
The point is that dirty attributes get persisted, not just the one you set. That is, the (invalid) username was saved because validations are skipped and the model as such is saved. Persisting dirty attributes is not an overlook, callbacks are run so you need to take the model as a whole and store it as a whole, with its current state, with all its consequences. The purpose of update_attribute was to save quick stuff, a flag toggle... that kind of things. Nowadays you'd generally use update_column. Xavier PS: Of course all this is documented, but we are discussing semantics, not documented behaviour. -- You received this message because you are subscribed to the Google Groups Ruby on Rails: Core group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-core+unsubscr...@googlegroups.com. To post to this group, send email to rubyonrails-core@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core. For more options, visit https://groups.google.com/d/optout.
Re: [Rails-core] update_attribute violates POLA
True. I somehow overlooked the fact that update_attribute saves dirty attributes. Xavier, you said the purpose of update_attribute was to save quick stuff, are there plans to deprecate this behavior? On Wed, Mar 12, 2014 at 4:10 PM, Xavier Noria f...@hashref.com wrote: The point is that dirty attributes get persisted, not just the one you set. That is, the (invalid) username was saved because validations are skipped and the model as such is saved. Persisting dirty attributes is not an overlook, callbacks are run so you need to take the model as a whole and store it as a whole, with its current state, with all its consequences. The purpose of update_attribute was to save quick stuff, a flag toggle... that kind of things. Nowadays you'd generally use update_column. Xavier PS: Of course all this is documented, but we are discussing semantics, not documented behaviour. -- You received this message because you are subscribed to the Google Groups Ruby on Rails: Core group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-core+unsubscr...@googlegroups.com. To post to this group, send email to rubyonrails-core@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core. For more options, visit https://groups.google.com/d/optout. -- Mohamed Wael Khobalatte -- You received this message because you are subscribed to the Google Groups Ruby on Rails: Core group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-core+unsubscr...@googlegroups.com. To post to this group, send email to rubyonrails-core@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core. For more options, visit https://groups.google.com/d/optout.
Re: [Rails-core] update_attribute violates POLA
Yeah, but it's kinda misleading that update_attribute saved other attributes and not just the one specified in the argument, being that the method is update_attribute as a singular attribute. The reload works fine, as the username was saved with update_attribute, which saved more than what it was told to. -- Sergio Campamá sergiocamp...@gmail.com On Wed, Mar 12, 2014 at 12:10 PM, Xavier Noria f...@hashref.com wrote: The point is that dirty attributes get persisted, not just the one you set. That is, the (invalid) username was saved because validations are skipped and the model as such is saved. Persisting dirty attributes is not an overlook, callbacks are run so you need to take the model as a whole and store it as a whole, with its current state, with all its consequences. The purpose of update_attribute was to save quick stuff, a flag toggle... that kind of things. Nowadays you'd generally use update_column. Xavier PS: Of course all this is documented, but we are discussing semantics, not documented behaviour. -- You received this message because you are subscribed to the Google Groups Ruby on Rails: Core group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-core+unsubscr...@googlegroups.com. To post to this group, send email to rubyonrails-core@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core. For more options, visit https://groups.google.com/d/optout. -- You received this message because you are subscribed to the Google Groups Ruby on Rails: Core group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-core+unsubscr...@googlegroups.com. To post to this group, send email to rubyonrails-core@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core. For more options, visit https://groups.google.com/d/optout.
Re: [Rails-core] update_attribute violates POLA
On Wed, Mar 12, 2014 at 4:21 PM, Mohamed Wael Khobalatte wael.khobala...@gmail.com wrote: True. I somehow overlooked the fact that update_attribute saves dirty attributes. Xavier, you said the purpose of update_attribute was to save quick stuff, are there plans to deprecate this behavior? It has been discussed, but update_attribute has been there for ages and by now there's no plan to deprecate it. Right now we consider update_attribute and update_column can coexist. I would be cool that the API of update_attribute mentioned update_column though. -- You received this message because you are subscribed to the Google Groups Ruby on Rails: Core group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-core+unsubscr...@googlegroups.com. To post to this group, send email to rubyonrails-core@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core. For more options, visit https://groups.google.com/d/optout.