Scoped databases
A Little tidbit from the rails mailing list:
In Shopify there are over 30 tables which store a shop_id. The Shop
object is figured out at the beginning of each request by looking at
the incoming domain of the user.
Product.find(:all) becomes shop.products.find(:all)
Product.new becomes shop.products.build
Since rails 0.13.1 we support calling class methods over associations.
This is actually a direct extraction out of shopify and makes such
databases much easier to handle.
class Product < AR:B
def self.search(q)
find(:all, :conditions => “title LIKE ‘#{q}’”)
end
end
Product.search(“snowboard”) will search in the entire database as
normal but shop.products.search(“snowboards”) will only search the
products with the appropriate shop_id.
This same change which allowed this also makes dynamic finders work on associations. shop.products.find_by_type(‘snowboard’) returns the expected results .