Hello, Is there a way to add automatic condition to all active record find methods? I have an attribute in all my tables something like "effective_end_date" so that I don''t delete records but I keep them by setting effective_end_date so I want to put condition is all my find queries effective_end_date Is null or effective_end_date > Time.now. I don''t want to type this for every single query so I was wondering if it''s possible to automate it. Thanks, Tam -- 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 -~----------~----~----~----~------~----~------~--~---
rubyguy-DaQTI0RpDDMAvxtiuMwx3w@public.gmane.org
2009-Mar-27 06:52 UTC
Re: adding automatic conditions to find method
On 27 Mar., 07:20, Tam Kbe <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hello, > > Is there a way to add automatic condition to all active record find > methods? > > I have an attribute in all my tables something like "effective_end_date" > so that I don''t delete records but I keep them by setting > effective_end_date so I want to put condition is all my find queries > effective_end_date Is null or effective_end_date > Time.now. I don''t > want to type this for every single query so I was wondering if it''s > possible to automate it.Yes, you can use default_scope: http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M002258 It supports the same options as the find methods. -- Best regards, David Knorr http://twitter.com/rubyguy --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thanks David..this is defenitaly the best solution but I realised that it needs rails 2.3.2 which I''m currently not using! I will try to upgrade but I''m worried I have to change lots of things. Do you know a way to do it in previous versions of rails? Thanks, Tam -- 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 -~----------~----~----~----~------~----~------~--~---