bramu.ss-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2010-Mar-30 02:27 UTC
overriding active record methods
Hi, My application is running fine but now I had an requirement like when somebody tries to delete the record from database using delete or delete_all I should not delete the record rather I should set the delete flag to 1. There are many model objects and there are many places I am using delete and delete_all method. I dont want to touch all the places rather I want to override the delete method of ActiveRecord::Base and also when application tries to get the records from database it should get only records those the delete flag set to 0. For this I have to override the base find method too. But I dont know how to override and where to override, and I dont want to add the overridden delete method in all the model objects at the same time. Please suggest the best solution for this. I am almost struck with this. please please help me on this.... Thanks, Ramu. -- 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.
bramu.ss-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2010-Mar-30 07:16 UTC
Re: overriding active record methods
anyone knows about this?? On Mar 30, 7:27 am, "bramu...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <bramu...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > My application is running fine but now I had an requirement like > when somebody tries to delete the record from database using delete or > delete_all I should not delete the record rather I should set the > delete flag to 1. There are many model objects and there are many > places I am using delete and delete_all method. I dont want to touch > all the places rather I want to override the delete method of > ActiveRecord::Base and also when application tries to get the records > from database it should get only records those the delete flag set to > 0. For this I have to override the base find method too. But I dont > know how to override and where to override, and I dont want to add the > overridden delete method in all the model objects at the same time. > Please suggest the best solution for this. I am almost struck with > this. please please help me on this.... > > Thanks, > Ramu.-- 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.
On 30 March 2010 03:27, bramu.ss-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <bramu.ss-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > My application is running fine but now I had an requirement like > when somebody tries to delete the record from database using delete or > delete_all I should not delete the record rather I should set the > delete flag to 1. There are many model objects and there are many > places I am using delete and delete_all method. I dont want to touch > all the places rather I want to override the delete method of > ActiveRecord::Base and also when application tries to get the records > from database it should get only records those the delete flag set to > 0. For this I have to override the base find method too. But I dont > know how to override and where to override, and I dont want to add the > overridden delete method in all the model objects at the same time. > Please suggest the best solution for this. I am almost struck with > this. please please help me on this....Have a look at acts_as_paranoid Colin -- 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.
Maybe you could find some plugins, but i could not remember which plugin could do this. sorry On Tue, Mar 30, 2010 at 3:16 PM, bramu.ss-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <bramu.ss-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>wrote:> anyone knows about this?? > > On Mar 30, 7:27 am, "bramu...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <bramu...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Hi, > > > > My application is running fine but now I had an requirement like > > when somebody tries to delete the record from database using delete or > > delete_all I should not delete the record rather I should set the > > delete flag to 1. There are many model objects and there are many > > places I am using delete and delete_all method. I dont want to touch > > all the places rather I want to override the delete method of > > ActiveRecord::Base and also when application tries to get the records > > from database it should get only records those the delete flag set to > > 0. For this I have to override the base find method too. But I dont > > know how to override and where to override, and I dont want to add the > > overridden delete method in all the model objects at the same time. > > Please suggest the best solution for this. I am almost struck with > > this. please please help me on this.... > > > > Thanks, > > Ramu. > > -- > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@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.
On 30 March 2010 03:27, bramu.ss-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <bramu.ss-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > My application is running fine but now I had an requirement like > when somebody tries to delete the record from database using delete or > delete_all I should not delete the record rather I should set the > delete flag to 1. There are many model objects and there are many > places I am using delete and delete_all method. I dont want to touch > all the places rather I want to override the delete method of > ActiveRecord::Base and also when application tries to get the records > from database it should get only records those the delete flag set to > 0. For this I have to override the base find method too. But I dont > know how to override and where to override, and I dont want to add the > overridden delete method in all the model objects at the same time. > Please suggest the best solution for this. I am almost struck with > this. please please help me on this.... >If you''ve used Model.find(:all, ....) you''re probably going to be a little stuck [1] , but if you''ve used Model.all, you can override this as a custom named scope in your model. named_scope :all, :conditions => "model_table.deleted_flag = false" Alternatively, maybe "default_scope :conditions => { :deleted_flag => false } " would be better for you. But you''d need to be aware of using "with_exclusive_scope" if you want to chain scopes and return the deleted records too. [1] You can do something along the lines of http://www.freezzo.com/2008/05/14/override-default-find-conditions-for-model/, but that makes it difficult to do a real "find all" - you''d probably have to alias the original method to a new name and remember to use it - not really conventional... -- 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.