Re: How to make a per-request variable globally available?

Aaron
2007-02-08T18:52:13Z

> Thank you, that''s exactly what I needed.

Your welcome

> class Widget < ActiveRecord::Base
>   class << self
>     # keep a pointer to the old find
>     alias :find_without_site :find
>
>     # extend find to always return widgets for the current site
>     def find(*args)
>       self.with_scope(:find => {:conditions => {:site_id =>
> Site.current.id}}) do
>         self.find_without_site(*args)
>       end
>     end
>   end
> end
>
> And it''s amazing. All unnecessary references to the site are gone
from
> my controllers, and even the other models. Thanks again!

Scoped finds are great, and overriding the default find will
automatically scope your associations too.  But be aware that the
find_by convenience methods (find_by_name, find_by_id, etc...) are NOT
scoped.

Aaron