'Implicit' subselection of data in find?


#1

Hi,

I’ve read about this technique on the great Intraweb but can’t find it
any more- hopefully someone can help or point me to a reference! I’d
hate to reinvent the wheel if I can avoid it (and rediscover all the
gotchas that someone has already found).

I’m writing a rails app that I’d like to use to service a number of
different websites out of the same running code base. So currently on
each request I have a before_filter that looks at the domain name, looks
up a table (called ‘sites’) and sets a global $site variable which I can
use in models, views and controllers to customise behaviour (including
localisation). So far so good.

Ideally I’d also like to tag each row in pretty much every table (there
are a few tables that are common across every site- such as localization
information that I don’t want to tag specific to a site) with the
site_id and then modify ‘find’ so that it searches only for that site’s
data. That way I can write most of my code ignoring the fact that I’m
actually only working on that site’s data.

I could explicitly specify the site_id in each find in every controller
(blech!). Or I should be able to override the find method and do it
automagically…

I don’t think the example I read was about different web sites running
out of the same code base. But it definitely tackled the same problem.

Many thanks!
Jeremy.


#2

Ok, to answer my own question, the magic words is ‘scope’ and it has
been extensively discussed on this mailing list. Best article on the use
of ‘with_scope’ seems to be:

http://habtm.com/articles/2006/02/22/nested-with_scope

Sorry for the noise! (I did look hard- honest!)

Thanks,
Jeremy.