Alexey M.
2011-Mar-29 18:37 UTC
ActiveRecord: when exactly is a record (model) saved to the database?
Hello, i am playing with some test application in console: creating a model, saving it, changing its id, trying to save it again, changing the id back, saving again, etc. The results are sometimes unexpected, but maybe i just do not understand how/when the records are saved. Can anybody please tell me if calling the "save" method saves the model immediately? If not, what is the method to save it immediately? I keep an Sqlite database browser open (a Firefox extension), and it seems that some "save" calls have no effect, but after changing some attributes, the "save" works again. I wonder if it is because of my messing around with id, or just the data base does not get updated immediately. Thanks. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Philip Hallstrom
2011-Mar-29 18:42 UTC
Re: ActiveRecord: when exactly is a record (model) saved to the database?
> i am playing with some test application in console: creating a model, > saving it, changing its id, trying to save it again, changing the idWhy are you changing it''s id? You probably shouldn''t be doing that...> back, saving again, etc. > The results are sometimes unexpected, but maybe i just do not understand > how/when the records are saved. > > Can anybody please tell me if calling the "save" method saves the model > immediately? > If not, what is the method to save it immediately? > > I keep an Sqlite database browser open (a Firefox extension), and it > seems that some "save" calls have no effect, but after changing some > attributes, the "save" works again. > I wonder if it is because of my messing around with id, or just the data > base does not get updated immediately.Do you have any validations on the model that might be failing? save will return false if validations fail, but won''t throw a fit... and obviously won''t save the record. Try save! which will throw a fit. Or ask the model for it''s errors... -philip> > Thanks. > > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2011-Mar-29 18:46 UTC
Re: ActiveRecord: when exactly is a record (model) saved to the database?
On 29 Mar 2011, at 19:37, "Alexey M." <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hello, > i am playing with some test application in console: creating a model, > saving it, changing its id, trying to save it again, changing the id > back, saving again, etc. > The results are sometimes unexpected, but maybe i just do not understand > how/when the records are saved. > > Can anybody please tell me if calling the "save" method saves the model > immediately?It will, as long as validations passed and at least some attributes have changed. Changing the id could conceivably cause odd things. Fred> If not, what is the method to save it immediately? > > I keep an Sqlite database browser open (a Firefox extension), and it > seems that some "save" calls have no effect, but after changing some > attributes, the "save" works again. > I wonder if it is because of my messing around with id, or just the data > base does not get updated immediately. > > Thanks. > > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Rodrigo Mendonça
2011-Mar-29 18:48 UTC
Re: ActiveRecord: when exactly is a record (model) saved to the database?
Use google to search for: around_save yield yield be inside the method around_save Everything is before yield is before save the record All is after yield is after save the record def around_save #do anything before save yield # do anything after save end 2011/3/29 Alexey M. <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>> Hello, > i am playing with some test application in console: creating a model, > saving it, changing its id, trying to save it again, changing the id > back, saving again, etc. > The results are sometimes unexpected, but maybe i just do not understand > how/when the records are saved. > > Can anybody please tell me if calling the "save" method saves the model > immediately? > If not, what is the method to save it immediately? > > I keep an Sqlite database browser open (a Firefox extension), and it > seems that some "save" calls have no effect, but after changing some > attributes, the "save" works again. > I wonder if it is because of my messing around with id, or just the data > base does not get updated immediately. > > Thanks. > > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- Rodrigo Mendonça (62) 8567-3142 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Alexey Muranov
2011-Mar-29 18:53 UTC
Re: ActiveRecord: when exactly is a record (model) saved to the database?
I do not have any validations, the model is basically empty (only attributes and associations). save! returns true, but doesn''t save anything, but saves later, after changing more attributes ... So, i assume this could be because of changing the id. But what the method id= is for then? I may post later (tomorrow?) a minimal example. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Robert Pankowecki (rupert)
2011-Mar-29 19:21 UTC
Re: ActiveRecord: when exactly is a record (model) saved to the database?
Maybe you are changing the attribute in place thus the change is not saved ? Read: "If an attribute is modified in-place then make use of [attribute_name]_will_change! to mark that the attribute is changing. Otherwise ActiveModel can’t track changes to in-place attributes." http://api.rubyonrails.org/classes/ActiveModel/Dirty.html Maybe you don''t see the change immediately because transaction is still going ? Robert Pankowecki -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Alexey Muranov
2011-Mar-29 19:41 UTC
Re: ActiveRecord: when exactly is a record (model) saved to the database?
Robert Pankowecki wrote in post #989839:> Maybe you are changing the attribute in place thus the change is not > saved ?This is possible, thanks for pointing this out, i will test. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2011-Mar-29 21:15 UTC
Re: Re: ActiveRecord: when exactly is a record (model) saved to the database?
On 29 Mar 2011, at 19:53, Alexey Muranov <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> I do not have any validations, the model is basically empty (only > attributes and associations). > > save! returns true, but doesn''t save anything, but saves later, after > changing more attributes ... > > So, i assume this could be because of changing the id. > But what the method id= is for then?I''ve only ever used it when I wanted a new record to have a specific id. Fred> I may post later (tomorrow?) a minimal example. > > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Alexey Muranov
2011-Mar-30 10:37 UTC
Re: ActiveRecord: when exactly is a record (model) saved to the database?
Robert, it does not seem like i was changing attributes in-place. Here is what is going on, i do not understand how it can be a desired behavior: I created a test application: $ rails new test_app $ cd test_app $ rails generate model Person name:string age:integer $ rake db:migrate In console:> p = Person.create(:name=>"Johny", :age=>30) > p.id # => 1 > p.id = 2 # => 2 > p.save # => trueNo change in the database!> p.id # => 2 > p.name = "Jim" # => "Jim" > p.save # => trueNo change in the database> p.name # => "Jim" > p.id = 1 # => 1 > p.save # => trueNo change in the database> p.age = 31 # => 31 > p.save # => trueIn the database, the age became 31, but the name stayed "Johny"!> p.name # => "Jim" > p.name.object_id # => 2155015240 > p.name = "Jim" # => "Jim" > p.name.object_id # => 2154887680 > p.save # => trueNo change in the database!> p.name = "Jimmy" # => "Jimmy" > p.name.object_id # => 2154859980 > p.save # => trueFinally, the name changed in the database! Alexey. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2011-Mar-30 10:50 UTC
Re: ActiveRecord: when exactly is a record (model) saved to the database?
On Mar 30, 11:37 am, Alexey Muranov <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Robert, it does not seem like i was changing attributes in-place. > > Here is what is going on, i do not understand how it can be a desired > behavior: >The core isssue is that changing id in activerecord is meaningless: every update statement is of the form (simplifying slightly) update blah set ... where id = #{self.id} So changing self.id will either update no rows or update some existing object. The second thing coming into play is dirty attributes: activerecord only includes changed attribute in the update clause, so if it thinks that no attributes have changed since the last save it won''t do anything. Why are you changing the id of an existing record? Fred> I created a test application: > > $ rails new test_app > $ cd test_app > $ rails generate model Person name:string age:integer > $ rake db:migrate > > In console: > > > p = Person.create(:name=>"Johny", :age=>30) > > p.id # => 1 > > p.id = 2 # => 2 > > p.save # => true> > No change in the database!> p.id # => 2 > > p.name = "Jim" # => "Jim" > > p.save # => true > > No change in the database> p.name # => "Jim" > > p.id = 1 # => 1 > > p.save # => true > > No change in the database> p.age = 31 # => 31 > > p.save # => true > > In the database, the age became 31, but the name stayed "Johny"!> p.name # => "Jim" > > p.name.object_id # => 2155015240 > > p.name = "Jim" # => "Jim" > > p.name.object_id # => 2154887680 > > p.save # => true > > No change in the database!> p.name = "Jimmy" # => "Jimmy" > > p.name.object_id # => 2154859980 > > p.save # => true > > Finally, the name changed in the database! > > Alexey. > > -- > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Alexey Muranov
2011-Mar-30 11:04 UTC
Re: ActiveRecord: when exactly is a record (model) saved to the database?
Frederick Cheung wrote in post #989934:> Why are you changing the id of an existing record?Just to learn how it behaves. I also plan an application where i want to use the primary key as a foreign key for a has_one association, so there it could (possibly) make sense to change it. If assigning anything to the primary key breaks ActiveRecord, is there a way to disable the id=() method after a record has been created? Alexey. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law
2011-Mar-30 11:17 UTC
Re: Re: ActiveRecord: when exactly is a record (model) saved to the database?
On 30 March 2011 12:04, Alexey Muranov <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Frederick Cheung wrote in post #989934: >> Why are you changing the id of an existing record? > > Just to learn how it behaves. > I also plan an application where i want to use the primary key as a > foreign key for a has_one association, so there it could (possibly) make > sense to change it.I told you life would be much easier for you if you stuck to the Rails conventions :) Colin> > If assigning anything to the primary key breaks ActiveRecord, is there a > way to disable the id=() method after a record has been created? > > Alexey. > > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Alexey Muranov
2011-Mar-30 11:24 UTC
Re: ActiveRecord: when exactly is a record (model) saved to the database?
Colin, i shall always listen to you better in the future. However, now i am taking this challenge personally. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Alexey Muranov
2011-Mar-30 12:15 UTC
Re: ActiveRecord: when exactly is a record (model) saved to the database?
Frederic, i think you are right, my problems/confusions are arising from the fact that on subsequent calls save uses UPDATE instead of INSERT. Is there a way to force it to use INSERT if the id is not used by another record? Alexey. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Alexey Muranov
2011-Mar-30 13:00 UTC
Re: ActiveRecord: when exactly is a record (model) saved to the database?
After some thinking, i agree that there is no real need to ever change the primary key (the reason i want to use it in my application also as a foreign key is exactly that there will be no need to change this foreign key). If i need to copy attributes from one record to another, i can do it with attributes=(). Thanks for all the explanations. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Alexey Muranov
2011-Mar-31 14:06 UTC
Re: ActiveRecord: when exactly is a record (model) saved to the database?
I am posting a related question in the same thread. It is more of a philosophical question. Can anybody please give me some philosophical explanation why the following behavior of ActiveRecord is considered ok (or should i submit a bug report/feature request?): In console:> p = Person.create(:name=>''Bill'') > p.destroy > p.name # => "Bill" > p.save # => trueNothing is saved in the database, but what disturbs me more is that "save" returned true in such case. A more elaborate version:> p = Person.create(:name=>''Bill'') > p.id # => 1 > pp = Person.find(1) > pp.destroyed > p.persisted? => true > p.destroyed? => false > p.save # => truebut the database is empty. Again, what bothers me the most is the "true" returned by "save". Alexey. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Bryan Crossland
2011-Apr-01 00:22 UTC
Re: Re: ActiveRecord: when exactly is a record (model) saved to the database?
On Thu, Mar 31, 2011 at 9:06 AM, Alexey Muranov <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>wrote:> I am posting a related question in the same thread. > It is more of a philosophical question. > Can anybody please give me some philosophical explanation why the > following behavior of ActiveRecord is considered ok (or should i submit > a bug report/feature request?): > > In console: > > p = Person.create(:name=>''Bill'') > > p.destroy > > p.name # => "Bill" > > p.save # => true > Nothing is saved in the database, but what disturbs me more is that > "save" returned true in such case. > >In this first example you are inializing an object instance of Person by calling the "create" method. The "create" method of class Person does a "create" to the database where as the method "new" does not. The "destroy" method you called sends a destroy to the database for the record based on that id but does not destroy the obect instance. This is because object is not a pointer to the database record, it''s an instance of the class Person. This is why you can still retrieve the name from your object instance later by calling the method "name". The "save" method should be doing the action of create or update in the database (depending on if the "new" or "create" methods were called to inialize the object instance). It returning "true" is strange since the record in the database is neither being created or updated. That may indeed be a bug.> A more elaborate version: > > p = Person.create(:name=>''Bill'') > > p.id # => 1 > > pp = Person.find(1) > > pp.destroyed > > p.persisted? => true > > p.destroyed? => false > > p.save # => true > but the database is empty. > Again, what bothers me the most is the "true" returned by "save". > >In this example you are inializing two seperate object instances of the class Person. One by calling the method "create" and the second by finding the first record in the database. Again, object instances are not pointers to the database record. The objects "p" and "pp" are totally seperate. Like above, activating the "destroy" method sends a destroy to the database based on the id in the object but does not destroy the calling object or any other object that happens to have the same id value. The "save" method returning true is strange since the record with the id value of 1 can''t be created again or updated. Someone else might know more as to why "save" returns true in this case. If not, then it is most likely a bug. B. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Michael Pavling
2011-Apr-01 05:50 UTC
Re: Re: ActiveRecord: when exactly is a record (model) saved to the database?
On 1 April 2011 01:22, Bryan Crossland <bacrossland-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Someone else might know more as to why "save" returns true in this case. If > not, then it is most likely a bug.Try it in your SQL console of choice: UPDATE my_table SET field1 = ''new value'' WHERE id = <non-existant-id-value> You''ll get a message "no records were updated" - that''s a successful execution as far as SQL is concerned. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2011-Apr-01 09:21 UTC
Re: ActiveRecord: when exactly is a record (model) saved to the database?
On Apr 1, 6:50 am, Michael Pavling <pavl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 1 April 2011 01:22, Bryan Crossland <bacrossl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Someone else might know more as to why "save" returns true in this case. If > > not, then it is most likely a bug. > > Try it in your SQL console of choice: > > UPDATE my_table SET field1 = ''new value'' WHERE id = <non-existant-id-value> > > You''ll get a message "no records were updated" - that''s a successful > execution as far as SQL is concerned.Although you might expect rails to check the numbers of rows modified (as it does when optimistic locking is enabled) - Select * from foo where id =''non existant'' will also execute just fine but rails chooses to make Foo.find(id) raise an exception in those cases Fred -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Michael Pavling
2011-Apr-01 10:03 UTC
Re: Re: ActiveRecord: when exactly is a record (model) saved to the database?
On 1 April 2011 10:21, Frederick Cheung <frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:>> UPDATE my_table SET field1 = ''new value'' WHERE id = <non-existant-id-value> >> >> You''ll get a message "no records were updated" - that''s a successful >> execution as far as SQL is concerned. > > Although you might expect rails to check the numbers of rows modified > (as it does when optimistic locking is enabled) - Select * from foo > where id =''non existant'' will also execute just fine but rails chooses > to make Foo.find(id) raise an exception in those casesI agree it probably breaks the principle of least surprise, so I''ve just had a rummage in active_record\base.rb (Rails 2.3.11) Obviously, .find returns rows, so it''s easy to raise an error if the size of the returned array is nil, but I was curious about the return value of create_or_update - so, when I try the previous example [1]:> p = Person.create(:name=>''Bill'') > p.destroy > p.name # => "Bill" > p.save # => trueIt works fine as the update method returns a true value if quoted_attributes is empty (as it uses that to build the set of fields to update - no fields to update == no query to run), but if you modify the object before saving (eg, > p.name = "William"), it errors with a "TypeError: can''t modify frozen hash" when trying to update the updated_at value because it''s frozen the @attributes_cache collection when destroy was called... So yes, it probably would make more sense to have .save return false or raise on saving a destroyed record - but it should raise if you try to alter a destroyed record, and it makes not much sense to save a record you''ve destroyed and not altered... :-/ I don''t know which way to plump... any thoughts? [1] That example is terribly contrived, and it''s making it hard for me to make up my mind if this behaviour is "bad" or not. Anyone got a real-world example of where this is causing a problem? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Alexey Muranov
2011-Apr-01 11:49 UTC
Re: ActiveRecord: when exactly is a record (model) saved to the database?
Fred, example [1] was meant to be a minimal example. I am more concerned about a situation when the same record is accessed by two applications or through two objects. I said that my question was philosophical (which does not mean that such behavior is not a bug). I understand why SQL executes "successfully", but if .save is only meant as a shortcut to a specific SQL, it should have been called differently, and in fact it should have been simply replaced by two methods: .insert and .update, which would return "true" if the SQL executed successfully, and there i wouldn''t have been surprised. The way it is, .save seems to hang awkwardly halfway between pure SQL (but not quite there because it verifies if the object has been saved before, which in many situation can be redundant, as the developer might know this in advance) and something more intelligent that would return "true" only if the object has been saved. Any more comments, or should i try to submit a bug report? Alexey. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Ramon Leon
2011-Apr-01 14:53 UTC
Re: Re: ActiveRecord: when exactly is a record (model) saved to the database?
On 04/01/2011 03:03 AM, Michael Pavling wrote:> So yes, it probably would make more sense to have .save return false > or raise on saving a destroyed record - but it should raise if you try > to alter a destroyed record, and it makes not much sense to save a > record you''ve destroyed and not altered... :-/ > > I don''t know which way to plump... any thoughts?Or.. make save work. Why should a deleted record be any different than a new unsaved record? When a record is destroyed it should lose its id and if saved again should insert a new row. At least, that''s what I''d expect, if the principle of least surprise means anything. -- Ramon Leon -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Bryan Crossland
2011-Apr-01 15:05 UTC
Re: Re: ActiveRecord: when exactly is a record (model) saved to the database?
On Fri, Apr 1, 2011 at 9:53 AM, Ramon Leon <ramon.leon-fDeA0g24QwDby3iVrkZq2A@public.gmane.org> wrote:> On 04/01/2011 03:03 AM, Michael Pavling wrote: > >> So yes, it probably would make more sense to have .save return false >> or raise on saving a destroyed record - but it should raise if you try >> to alter a destroyed record, and it makes not much sense to save a >> record you''ve destroyed and not altered... :-/ >> >> I don''t know which way to plump... any thoughts? >> > > Or.. make save work. Why should a deleted record be any different than a > new unsaved record? When a record is destroyed it should lose its id and if > saved again should insert a new row. At least, that''s what I''d expect, if > the principle of least surprise means anything. > >Except in this case your "record" is an object instance. Since an object instance is not a pointer to the actual record in the database it should not change when you destroy that record. Keep in mind that there are many different ways to destroy a record in a database that don''t involve calling the .destroy method of the object instance. What should happen is that the .save method should return false if it can''t insert or update the record by that id. From following the thread it looks like its actually checking for an SQL execution error and it is not receiving one from the database. What its getting back is a successful execution of the SQL statement but a message that states the there was no record to create/update. From this information it then returns the incorrect state of true. B. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Ramon Leon
2011-Apr-01 15:29 UTC
Re: Re: ActiveRecord: when exactly is a record (model) saved to the database?
On 04/01/2011 08:05 AM, Bryan Crossland wrote:> Except in this case your "record" is an object instance.Of course.> Since an object instance is not a pointer to the actual record in the database > it should not change when you destroy that record.That doesn''t follow. If there is no longer a row 1 in the database, the object should no longer reference row 1. Currently the object is left in an invalid state and save thinks an update is possible when it isn''t. The abstraction is leaking.>Keep in mind that there are many > different ways to destroy a record in a database that don''t involve calling > the .destroy method of the object instance.Not relevant to this case.>What should happen is that the > .save method should return false if it can''t insert or update the record by > that id.At a minimum, agreed; it should show the affected row count. But success is better than failure and you still haven''t acknowledged that a destroyed object is conceptually identical to a new object that hasn''t been saved and logically, should behave the same. Save could insert a new record and it would perfectly conceptually elegant and plug the leaking abstraction.> From following the thread it looks like its actually checking for > an SQL execution error and it is not receiving one from the database. What > its getting back is a successful execution of the SQL statement but a > message that states the there was no record to create/update. From this > information it then returns the incorrect state of true.The current behavior is clearly wrong of course. -- Ramon Leon -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2011-Apr-01 15:29 UTC
Re: ActiveRecord: when exactly is a record (model) saved to the database?
On Apr 1, 3:53 pm, Ramon Leon <ramon.l...-fDeA0g24QwDby3iVrkZq2A@public.gmane.org> wrote:> On 04/01/2011 03:03 AM, Michael Pavling wrote: > > > So yes, it probably would make more sense to have .save return false > > or raise on saving a destroyed record - but it should raise if you try > > to alter a destroyed record, and it makes not much sense to save a > > record you''ve destroyed and not altered... :-/ > > > I don''t know which way to plump... any thoughts? > > Or.. make save work. Why should a deleted record be any different than > a new unsaved record? When a record is destroyed it should lose its id > and if saved again should insert a new row. At least, that''s what I''d > expect, if the principle of least surprise means anything. >That sounds dangerous to me. If someone decided to destroy a record, resurrecting it because in a narrow window of opportunity someone tried to update it sounds like it would lead to very difficult to track down bugs. For save to fail/raise an exception would be less dangerous Fred> -- > Ramon Leon-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Bryan Crossland
2011-Apr-01 16:11 UTC
Re: Re: ActiveRecord: when exactly is a record (model) saved to the database?
On Fri, Apr 1, 2011 at 10:29 AM, Ramon Leon <ramon.leon-fDeA0g24QwDby3iVrkZq2A@public.gmane.org>wrote:> On 04/01/2011 08:05 AM, Bryan Crossland wrote: > >> Except in this case your "record" is an object instance. >> > > Of course. > > > Since an object instance is not a pointer to the actual record in the >> database >> it should not change when you destroy that record. >> > > That doesn''t follow. If there is no longer a row 1 in the database, the > object should no longer reference row 1. Currently the object is left in an > invalid state and save thinks an update is possible when it isn''t. The > abstraction is leaking.> Keep in mind that there are many >> different ways to destroy a record in a database that don''t involve >> calling >> the .destroy method of the object instance. >> > > Not relevant to this case. > > > What should happen is that the >> .save method should return false if it can''t insert or update the record >> by >> that id. >> > > At a minimum, agreed; it should show the affected row count. But success > is better than failure and you still haven''t acknowledged that a destroyed > object is conceptually identical to a new object that hasn''t been saved and > logically, should behave the same. Save could insert a new record and it > would perfectly conceptually elegant and plug the leaking abstraction. > > > From following the thread it looks like its actually checking for >> an SQL execution error and it is not receiving one from the database. What >> its getting back is a successful execution of the SQL statement but a >> message that states the there was no record to create/update. From this >> information it then returns the incorrect state of true. >> > > The current behavior is clearly wrong of course. > >I see your point. Without being able to modify the object after the .destroy call it becomes a read-only object of no value. However, being able to salvage that object as "new" one would solve the issue. I like it. B. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Alexey Muranov
2011-Apr-01 16:52 UTC
Re: ActiveRecord: when exactly is a record (model) saved to the database?
From my point of view, the most natural way to reestablish the "least surprise" principle would be to 1. implement .insert and .update methods which would simply execute SQL and return it''s errors. 2. abandon the .destroyed? method and its @destroyed instance variable 3. switch .persisted? (@persisted) to false when the record is destroyed or if its .id is manually changed in the program 4. implement .save as something intelligent. Of course such drastic changes might not seem practical... Alexey. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Alexey Muranov
2011-Apr-03 11:38 UTC
Re: ActiveRecord: when exactly is a record (model) saved to the database?
I have created a ticket about this at Lighthouse, please comment: https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/6666-save-activerecordpersistence-can-return-true-even-when-the-model-is-not-saved-and-seems-to-break-the-principle-of-least-surprise -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.