How to make a per-request variable globally available?

  # makes @site available to all controllers and views   def initialize_site     @site = Site.find_by_domain(request.domain)   end end

You can use Thread.current to pass data to your models. Something like this:

def initialize_site   @site = Site.find_by_domain(request.domain)   Thread.current[:site] = @site end

Aaron

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