collectiveidea/awesome_nested_set — Issue #105: awesome_nested_sets won't work with factory_girl.
Author: hsanson
Created: 2011-11-22 11:02:21+00:00
State: open

I want to use factory_girl for my tests but for some reason awesome_nested_sets won't work. On my application all controller, model and view work as expected with awesome_nested_sets.

I am using latest awesome_nested_sets cloned from master and latest factory_girl_rails gem.

This is my Label class

class Label < ActiveRecord::Base
attr_protected :lft, :rgt
acts_as_nested_set :dependent => :destroy, :counter_cache => :children_count
belongs_to :user
belongs_to :labelable, :polymorphic => true

validates_presence_of :user
validates_presence_of :labelable
validates_presence_of :name
end

The defined Factory

Factory.define :label do |f|
f.name "Label"
f.association :user
f.association :labelable, :factory => :channel
end

Then in my test calling :

Factory.create(:label)

results in nil.+ NoMethodError:

NoMethodError: You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.+
from /home/ryujin/.bundler/ruby/1.9.1/awesome_nested_set-7f43579770dd/lib/awesome_nested_set/awesome_nested_set.rb:418:in `set_default_left_and_right'
from /var/lib/gems/1.9.1/gems/activesupport-3.1.3/lib/active_support/callbacks.rb:395:in `_run_create_callbacks'
from /var/lib/gems/1.9.1/gems/activesupport-3.1.3/lib/active_support/callbacks.rb:81:in `run_callbacks'
from /var/lib/gems/1.9.1/gems/activerecord-3.1.3/lib/active_record/callbacks.rb:268:in `create'
from /var/lib/gems/1.9.1/gems/activerecord-3.1.3/lib/active_record/persistence.rb:294:in `create_or_update'
from /var/lib/gems/1.9.1/gems/activerecord-3.1.3/lib/active_record/callbacks.rb:264:in `block in create_or_update'
from /var/lib/gems/1.9.1/gems/activesupport-3.1.3/lib/active_support/callbacks.rb:435:in `_run_save_callbacks'
from /var/lib/gems/1.9.1/gems/activesupport-3.1.3/lib/active_support/callbacks.rb:81:in `run_callbacks'
from /var/lib/gems/1.9.1/gems/activerecord-3.1.3/lib/active_record/callbacks.rb:264:in `create_or_update'
from /var/lib/gems/1.9.1/gems/activerecord-3.1.3/lib/active_record/persistence.rb:37:in `save'
from /var/lib/gems/1.9.1/gems/activerecord-3.1.3/lib/active_record/validations.rb:50:in `save'
from /var/lib/gems/1.9.1/gems/activerecord-3.1.3/lib/active_record/attribute_methods/dirty.rb:22:in `save'
from /var/lib/gems/1.9.1/gems/activerecord-3.1.3/lib/active_record/transactions.rb:241:in `block (2 levels) in save'
from /var/lib/gems/1.9.1/gems/activerecord-3.1.3/lib/active_record/transactions.rb:295:in `block in with_transaction_returning_status'
from /var/lib/gems/1.9.1/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
from /var/lib/gems/1.9.1/gems/activerecord-3.1.3/lib/active_record/transactions.rb:208:in `transaction'
... 34 levels...
from /var/lib/gems/1.9.1/gems/factory_girl-2.3.0/lib/factory_girl/factory.rb:198:in `handle_attribute_without_overrides'
from /var/lib/gems/1.9.1/gems/factory_girl-2.3.0/lib/factory_girl/factory.rb:173:in `block in apply_attributes'
from /var/lib/gems/1.9.1/gems/factory_girl-2.3.0/lib/factory_girl/attribute_list.rb:18:in `each'
from /var/lib/gems/1.9.1/gems/factory_girl-2.3.0/lib/factory_girl/attribute_list.rb:18:in `each'
from /var/lib/gems/1.9.1/gems/factory_girl-2.3.0/lib/factory_girl/factory.rb:169:in `apply_attributes'
from /var/lib/gems/1.9.1/gems/factory_girl-2.3.0/lib/factory_girl/factory.rb:160:in `run'
from /var/lib/gems/1.9.1/gems/factory_girl-2.3.0/lib/factory_girl/factory.rb:45:in `run'
from /var/lib/gems/1.9.1/gems/factory_girl-2.3.0/lib/factory_girl/syntax/methods.rb:148:in `run_factory_girl_proxy'
from /var/lib/gems/1.9.1/gems/factory_girl-2.3.0/lib/factory_girl/syntax/methods.rb:63:in `create'
from /var/lib/gems/1.9.1/gems/factory_girl-2.3.0/lib/factory_girl/syntax/vintage.rb:124:in `create'
from (irb):2
from /var/lib/gems/1.9.1/gems/railties-3.1.3/lib/rails/commands/console.rb:45:in `start'
from /var/lib/gems/1.9.1/gems/railties-3.1.3/lib/rails/commands/console.rb:8:in `start'
from /var/lib/gems/1.9.1/gems/railties-3.1.3/lib/rails/commands.rb:40:in `'
from script/rails:6:in `require'