Forum: Ruby on Rails Extending finders to include a session variable as an option??

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.
Alan (Guest)
on 2009-05-20 02:16
(Received via mailing list)
We have a Page model which acts_as_paranoid.

AAP modifies ActiveRecord finders to ignore any records marked as
deleted unless the :with_deleted => true option is supplied.

We have a UI element which allows users to toggle whether or not to
display deleted Pages, their current setting being stored in a session
variable, session[:show_deleted].

What we would like is to automagically have :with_deleted => session
[:show_deleted] appended to all calls to find, all, first, find_by_
etc.

That would be easy enough to do with an alias_method_chain but for the
fact that session data is not available when the call is being made
from a model.

I know accessing session data in a model is controversial, but can
anyone suggest a workaround? Or alternatively another approach to
being able to easily switch between including/ignoring deleted records
in all finder methods?

Thanks.
This topic is locked and can not be replied to.