Vikrant
2009-Jan-16 15:02 UTC
database changes aren''t happening in callback before_destroy()
I have a model Model1 with the before_destory() method defined as below. def before_destroy self.status = ''deleted'' self.save! return false #don''t want to delete the row. end But in the database the change in the status column doesn''t get changed to ''deleted'' I then tried this def before_destory self.status = ''deleted'' self.save! n = Model1.find self.id #Set the breakpoint here in NetBeans IDE. return false end I then checked the value of n.status and it was ''deleted'', but in the database it was still ''active'' (the other value). I also tried this Controller1 < ApplicationController def action1 m = Model1.find params[:id] m.destroy n = Model1.find m.id #Set the breakpoint here end end Even here the value of n.status was ''deleted'', but in the database it was the previous value. (Showed this example just to show that even in controller I''m getting the same). After a bit of of looking through Rails API, I found out that model callbacks like before_destroy are transactional. so they get rolled back if any error raised. So I tried this. def before_destroy self.status = ''deleted'' self.save! raise self.errors.inspect return false end The above code made sure that there are no errors attached, so self.save! should run fine (or at least it should raise error!). So I don''t know now, why my changes are not being written to the database. Is it because I''m returning false from the before_destroy() method? If yes, then how can I save the changes made in before_destroy () callback and also the row doesn''t get deleted? I''m using SQLite and I''ve also noticed that a ''development-journal'' file get created, while I''ve paused the code somewhere in between the before_destroy() method. (That may be the transaction thing, I guess.) I''m using - Rails 2.2.2 SQLite 3.5.9 Ubuntu 8.10 (Intrepid) x64 --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2009-Jan-16 15:32 UTC
Re: database changes aren''t happening in callback before_destroy()
On 16 Jan 2009, at 15:02, Vikrant wrote:> > I have a model Model1 with the before_destory() method defined as > below. > > def before_destroy > self.status = ''deleted'' > self.save! > return false #don''t want to delete the row. > end > > But in the database the change in the status column doesn''t get > changed to ''deleted'' > > I then tried this > > def before_destory > self.status = ''deleted'' > self.save! > n = Model1.find self.id #Set the breakpoint here in NetBeans IDE. > return false > endHave you tried looking at development log to see exactly what queries run ? Fred> > > I then checked the value of n.status and it was ''deleted'', but in the > database it was still ''active'' (the other value). > > I also tried this > > Controller1 < ApplicationController > def action1 > m = Model1.find params[:id] > m.destroy > n = Model1.find m.id #Set the breakpoint here > end > end > > Even here the value of n.status was ''deleted'', but in the database it > was the previous value. (Showed this example just to show that even in > controller I''m getting the same). > > After a bit of of looking through Rails API, I found out that model > callbacks like before_destroy are transactional. so they get rolled > back if any error raised. So I tried this. > > def before_destroy > self.status = ''deleted'' > self.save! > raise self.errors.inspect > return false > end > > The above code made sure that there are no errors attached, so > self.save! should run fine (or at least it should raise error!). > So I don''t know now, why my changes are not being written to the > database. Is it because I''m returning false from the before_destroy() > method? If yes, then how can I save the changes made in before_destroy > () callback and also the row doesn''t get deleted? > > I''m using SQLite and I''ve also noticed that a ''development-journal'' > file get created, while I''ve paused the code somewhere in between the > before_destroy() method. (That may be the transaction thing, I guess.) > > I''m using - > Rails 2.2.2 > SQLite 3.5.9 > Ubuntu 8.10 (Intrepid) x64 > > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Vikrant
2009-Jan-16 15:42 UTC
Re: database changes aren''t happening in callback before_destroy()
Yes, just what it should be - UPDATE "model1" SET "updated_at" = .........., "status" = "deleted" WHERE id = 40 but still the database isn''t reflecting that. Also I can not find any transaction rollback specific information in the log, as this is the last query to the database for that HTTP request. On Jan 16, 8:32 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 16 Jan 2009, at 15:02, Vikrant wrote: > > > > > > > I have a model Model1 with the before_destory() method defined as > > below. > > > def before_destroy > > self.status = ''deleted'' > > self.save! > > return false #don''t want to delete the row. > > end > > > But in the database the change in the status column doesn''t get > > changed to ''deleted'' > > > I then tried this > > > def before_destory > > self.status = ''deleted'' > > self.save! > > n = Model1.find self.id #Set the breakpoint here in NetBeans IDE. > > return false > > end > > Have you tried looking at development log to see exactly what queries > run ? > > Fred > > > > > I then checked the value of n.status and it was ''deleted'', but in the > > database it was still ''active'' (the other value). > > > I also tried this > > > Controller1 < ApplicationController > > def action1 > > m = Model1.find params[:id] > > m.destroy > > n = Model1.find m.id #Set the breakpoint here > > end > > end > > > Even here the value of n.status was ''deleted'', but in the database it > > was the previous value. (Showed this example just to show that even in > > controller I''m getting the same). > > > After a bit of of looking through Rails API, I found out that model > > callbacks like before_destroy are transactional. so they get rolled > > back if any error raised. So I tried this. > > > def before_destroy > > self.status = ''deleted'' > > self.save! > > raise self.errors.inspect > > return false > > end > > > The above code made sure that there are no errors attached, so > > self.save! should run fine (or at least it should raise error!). > > So I don''t know now, why my changes are not being written to the > > database. Is it because I''m returning false from the before_destroy() > > method? If yes, then how can I save the changes made in before_destroy > > () callback and also the row doesn''t get deleted? > > > I''m using SQLite and I''ve also noticed that a ''development-journal'' > > file get created, while I''ve paused the code somewhere in between the > > before_destroy() method. (That may be the transaction thing, I guess.) > > > I''m using - > > Rails 2.2.2 > > SQLite 3.5.9 > > Ubuntu 8.10 (Intrepid) x64--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
Ilan Berci
2009-Jan-16 15:53 UTC
Re: database changes aren''t happening in callback before_destroy()
Vikrant wrote:> I have a model Model1 with the before_destory() method defined as > below. > > def before_destroy > self.status = ''deleted'' > self.save! > return false #don''t want to delete the row. > end >Your transaction isn''t completed until the request is completed. If you check your DB, you will not see any changes until the request has been completed (assuming that your transaction level is beyond dirty reads) Secondly, you shouldn''t save in a before_destroy filter I also believe that returning false will abort the transaction and you will be left with an unmodified data source. ilan -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Vikrant
2009-Jan-16 16:12 UTC
Re: database changes aren''t happening in callback before_destroy()
Well, I can''t find the problem, but I guess I have an alternative solution, and that is to use a new method named Model1.make_deleted() which does the deletion work. Also, then I should stop destory() from being executed (by raising some kind of exception in before_destroy() method). I guess that shall do the work, but I''m still thinking if there is a standard way of marking a row as deleted, while not actually deleting the row, since the above solution doesn''t seem to be in compliance with Rails-way. (Callbacks are made for some purpose after all). On Jan 16, 8:53 pm, Ilan Berci <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Vikrant wrote: > > I have a model Model1 with the before_destory() method defined as > > below. > > > def before_destroy > > self.status = ''deleted'' > > self.save! > > return false #don''t want to delete the row. > > end > > Your transaction isn''t completed until the request is completed. If you > check your DB, you will not see any changes until the request has been > completed (assuming that your transaction level is beyond dirty reads) > > Secondly, you shouldn''t save in a before_destroy filter > > I also believe that returning false will abort the transaction and you > will be left with an unmodified data source. > > ilan > > -- > 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---