Global Scope based on Subdomain


#1

Hi, I’m a noob trying to figure out the best way to limit queries by a
particular condition that is dependent on the subdomain of the site.

I have a table “places” with a field called “subdomain”. Say I have
another table that belongs to places called “knights” (contains
“place_id”). Like so:

places table - Place.id, Place.subdomain
knights table - Knight.id, Knight.place_id

On the site, “heredia.knightsofni.es”, I only want to show the
knights who live in Heredia wherever knights are shown.

My questions are these:

  1. What is the best way to set the “active” place globally? Right now
    I am setting an instance variable @active_place in
    application_controller.rb like so: @active_place = Place.find
    (:first, :conditions => [‘places = ?’, subdomain]). Is this OK, or is
    there a better way so that the @active_place data is more globally
    available?

  2. How can I constrain by place while staying RESTful? Right now I’m
    putting if/then statements in my controller methods like so: if
    @active_place { @knights = Knight.by_place } else { @knights =
    Knight.all }

Thanks for the help,
Ni!


#2

Basically I need a default_scope that constrains by kingdom. Is there
some way to do something like this?:

class Knight < ActiveRecord::Base
belongs_to :kingdom
default_scope :include => :kingdom, :conditions =>
[‘kingdoms.subdomain = ?’, current_subdomain]

But I see no way of getting the current subdomain inside the model.

Thanks (Ni!)