I’m making use of a global variable to do dynamic scoping in my models.
For example, here is how one model is defined:
class Invitation < ActiveRecord::Base
belongs_to :site
Scope all find calls to the current site_id.
scoped_methods << {
:find => { :conditions => [ ‘site_id = ?’,
$site_id ] }
}
end
In my ApplicationController, $site_id is set in a before_filter which
determines which subdomain the request came from. With this, I can
perform finds on my models without having to pass in the site_id every
time. This works well, although I understand it violates the MVC
philosophy - please bear with me.
It works well, except for tests. Actually, it works in tests if I
manually set $site_id, but only if I am not loading a fixture for
this model. For example, the following actually works (note that my
fixtures are commented out):
require File.dirname(FILE) + ‘/…/test_helper’
class InvitationTest < Test::Unit::TestCase
#fixtures :invitations
def test_global_wierdness
$site_id = 1
i = Invitation.find_by_last_name("Jones")
assert_valid i
end
end
So, if I run this test and watch my test.log file, I can verify that the
SQL query is run with site_id=1. All is good.
However, if I uncomment the fixtures line and watch my test.log file, I
find that $site_id is no longer visible within the model and my SQL
query ends up searching for site_id=NULL.
What exactly is going on here? Does loading a fixture somehow clobber
global variables? I’ve tried renaming the global $site_id to something
different (since that’s also a field in my Invitations table) but that
makes no difference.
Also, I can load fixtures for other models and the global variable
still works. It’s only when I load a fixture for the Invitation model
that it disappears.
I’ve been struggling with this problem for many hours and would be
greatly indebted to anyone who could educate me.
Thanks,
Scott