# Testing and Fixtures with relationships issue

**URL:** <https://www.ruby-forum.com/t/testing-and-fixtures-with-relationships-issue/121507>\
**Category:** Rails\
**Created:** [November 28, 2007, 3:42pm UTC](https://www.ruby-forum.com/t/testing-and-fixtures-with-relationships-issue/121507 "2007-11-28T15:42:54Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![JesSSSSs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cdc98d/32.png) [@JesSSSSs](https://www.ruby-forum.com/u/JesSSSSs)\
**Post date:** [November 28, 2007, 3:42pm UTC](https://www.ruby-forum.com/t/testing-and-fixtures-with-relationships-issue/121507/1 "2007-11-28T15:42:54Z")

</div>

Hi, i just started in rails and i’m very stuck with a fixture issue.

I’m trying to test my models, i have the following models:  
-user: tipical user  
-event: “something” with datetime and other things (for example,  
holydays, trips, and so on)  
-event\_type: defines type of events and some options.

The user has\_many :events  
The event belongs\_to :user, and also belongs\_to :event\_type  
The event\_type has\_many :events

But! (here it’s the thing) I’ve defined other relationships, in  
‘User’, like that:  
has\_many :vacations ,  
:class\_name =\> “Event”,  
:foreign\_key =\> “user\_id”,  
:conditions =\> [‘event\_type\_id = ?’, EventType.vacations.id]

Obviously in ‘EventType’ i have methods like:  
def self.vacations  
find(:first, :conditions =\> [“name = ?”, “vacations”])  
end

Ok, now when i try to execute something like:  
require File.dirname( **FILE** ) + ‘/…/test\_helper’  
class UserTest \< Test::Unit::TestCase  
fixtures :event\_types, :events, :users  
def test\_users  
assert true  
end  
end

I obtained the following error:

$ rake test:units  
(in /home/blaxter/dev/rails/railsAppSample)  
/usr/bin/ruby1.8 -Ilib:test  
“/usr/lib/ruby/1.8/rake/rake\_test\_loader.rb” “test/unit/user\_test.rb”  
/home/blaxter/dev/rails/railsAppSample/config/…/app/models/user.rb:14:  
Called id for nil, which would mistakenly be 4 – if you really wanted  
the id of nil, use object\_id (RuntimeError)  
from /usr/lib/ruby/1.8/rubygems/custom\_require.rb:27:in  
`gem_original_require' from /usr/lib/ruby/1.8/rubygems/custom_require.rb:27:in`require’  
from  
/home/blaxter/dev/rails/railsAppSample/config/…/vendor/rails/activerecord/lib/…/…/activesupport/lib/active\_support/dependencies.rb:496:in  
`require' from /home/blaxter/dev/rails/railsAppSample/config/../vendor/rails/activerecord/lib/../../activesupport/lib/active_support/dependencies.rb:343:in`new\_constants\_in’  
from  
/home/blaxter/dev/rails/railsAppSample/config/…/vendor/rails/activerecord/lib/…/…/activesupport/lib/active\_support/dependencies.rb:496:in  
`require' from /home/blaxter/dev/rails/railsAppSample/config/../vendor/rails/activerecord/lib/../../activesupport/lib/active_support/dependencies.rb:105:in`require\_or\_load’  
from  
/home/blaxter/dev/rails/railsAppSample/config/…/vendor/rails/activerecord/lib/…/…/activesupport/lib/active\_support/dependencies.rb:61:in  
`depend_on' from /home/blaxter/dev/rails/railsAppSample/config/../vendor/rails/activerecord/lib/../../activesupport/lib/active_support/dependencies.rb:443:in`require\_dependency’  
from  
/home/blaxter/dev/rails/railsAppSample/config/…/vendor/rails/activerecord/lib/active\_record/fixtures.rb:472:in  
`require_fixture_classes' from /home/blaxter/dev/rails/railsAppSample/config/../vendor/rails/activerecord/lib/active_record/fixtures.rb:468:in`each’  
from  
/home/blaxter/dev/rails/railsAppSample/config/…/vendor/rails/activerecord/lib/active\_record/fixtures.rb:468:in  
`require_fixture_classes' from /home/blaxter/dev/rails/railsAppSample/config/../vendor/rails/activerecord/lib/active_record/fixtures.rb:463:in`fixtures’  
from ./test/unit/user\_test.rb:6  
from /usr/lib/ruby/1.8/rake/rake\_test\_loader.rb:5:in `load' from /usr/lib/ruby/1.8/rake/rake_test_loader.rb:5 from /usr/lib/ruby/1.8/rake/rake_test_loader.rb:5:in`each’  
from /usr/lib/ruby/1.8/rake/rake\_test\_loader.rb:5  
rake aborted!  
Command failed with status (1): [/usr/bin/ruby1.8 -Ilib:test  
"/usr/lib/ruby…]

I thing the error is because rails is trying to obtain ‘vacations’  
relationship before ‘EventType’ table contains the data defined in the  
fixture. The line 14 of users.rb is: :conditions =\> [‘event\_type\_id =  
?’, EventType.vacations.id] so EventType.vacations will be nil, so  
EventType.vacations.id fails

I try to defined the fixtures in order, but it doesn’t work.

I don’t know if i’m doing something wrong, if i shouldn’t use that  
kind of relathionship in my model, or what i should try to do :(.

Any help it would appreciate, thanks.  
Jesús.
