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.