# Loading a fixture clobbers a global variable

**URL:** <https://www.ruby-forum.com/t/loading-a-fixture-clobbers-a-global-variable/116060>\
**Category:** Rails\
**Created:** [October 1, 2007, 11:49pm UTC](https://www.ruby-forum.com/t/loading-a-fixture-clobbers-a-global-variable/116060 "2007-10-01T23:49:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![zenlinuxnh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/b4bc9f/32.png) [@zenlinuxnh](https://www.ruby-forum.com/u/zenlinuxnh)\
**Post date:** [October 1, 2007, 11:49pm UTC](https://www.ruby-forum.com/t/loading-a-fixture-clobbers-a-global-variable/116060/1 "2007-10-01T23:49:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![zenlinuxnh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/b4bc9f/32.png) [@zenlinuxnh](https://www.ruby-forum.com/u/zenlinuxnh)\
**Post date:** [October 2, 2007, 12:06am UTC](https://www.ruby-forum.com/t/loading-a-fixture-clobbers-a-global-variable/116060/2 "2007-10-02T00:06:06Z")

</div>

On 1 Oct 2007, at 22:49, Scott Garman wrote:

> 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.

Rails overrides the setup method, and this is where the fixtures are  
loaded (ie before the code in test\_global\_weirdness runs). This  
causes invitation.rb to be loaded. At this point $site\_id is not set  
and so when scoped\_methods \<\< {:find =\> { :conditions =\> [ ‘site\_id  
= ?’,$site\_id ] } } is evaluated, $site\_id is nil.  
If you don’t have invitations as a fixture, then invitation.rb is  
only loaded when Invitation is used, i.e. after you’ve set $site\_id.  
So it’s not that fixtures are clobbering anything. You’ll also have  
problems if different tests try to set $site\_id to different values.  
Fundamentally this approach seems unwise. If you absolutely had to,  
you could set $site\_id in test\_helper.rb

Fred

---

<div class="post-metadata">

**Author:** ![zenlinuxnh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/b4bc9f/32.png) [@zenlinuxnh](https://www.ruby-forum.com/u/zenlinuxnh)\
**Post date:** [October 2, 2007, 4:20am UTC](https://www.ruby-forum.com/t/loading-a-fixture-clobbers-a-global-variable/116060/3 "2007-10-02T04:20:24Z")

</div>

Frederick C. wrote:

> Rails overrides the setup method, and this is where the fixtures are  
> loaded (ie before the code in test\_global\_weirdness runs). This  
> causes invitation.rb to be loaded. At this point $site\_id is not set  
> and so when scoped\_methods \<\< {:find =\> { :conditions =\> [ ‘site\_id  
> = ?’,$site\_id ] } } is evaluated, $site\_id is nil.

Fred, I can’t thank you enough for such a clear explanation. I’m  
evaluating other ways of scoping my models, but you’ve resolved this  
particular mystery.

Thanks,

Scott
