Forum: Ruby on Rails Global Scope based on Subdomain

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
06be1f67bda5e2c0315e20cf056a0aef?d=identicon&s=25 KnightOfNi (Guest)
on 2009-06-04 09:47
(Received via mailing list)
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!
06be1f67bda5e2c0315e20cf056a0aef?d=identicon&s=25 KnightOfNi (Guest)
on 2009-06-05 00:54
(Received via mailing list)
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!)
This topic is locked and can not be replied to.