Forum: Ruby on Rails adding automatic conditions to find method

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
330edb8dc8bbc5a824984cf46e941b2f?d=identicon&s=25 Tam Kbe (tkbeili)
on 2009-03-27 07:20
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
36ed63ef68c172f148c491418395d0b5?d=identicon&s=25 rubyguy@ymail.com (Guest)
on 2009-03-27 07:53
(Received via mailing list)
On 27 Mar., 07:20, Tam Kbe <rails-mailing-l...@andreas-s.net> wrote:
> possible to automate it.
Yes, you can use default_scope:
http://api.rubyonrails.org/classes/ActiveRecord/Ba...
It supports the same options as the find methods.

--
Best regards,
David Knorr
http://twitter.com/rubyguy
330edb8dc8bbc5a824984cf46e941b2f?d=identicon&s=25 Tam Kbe (tkbeili)
on 2009-03-28 16:22
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
This topic is locked and can not be replied to.