Skip to content

Instantly share code, notes, and snippets.

@r00k
Created November 29, 2011 16:49
Show Gist options
  • Save r00k/1405490 to your computer and use it in GitHub Desktop.
Save r00k/1405490 to your computer and use it in GitHub Desktop.
class Cycle < ActiveRecord::Base
belongs_to :user
belongs_to :decision, polymorphic: true
validates :user_id, presence: true
validates :decision_id, presence: true
validates :decision_type, presence: true
validates :effective_at, presence: true
end
FactoryGirl.define do
#...
factory :rental do
product
end
factory :cycle do
user
decision rental
effective_at { Date.beginning_of_this_month }
end
end
class Rental < ActiveRecord::Base
has_one :cycle, as: :decision
belongs_to :product
end
@r00k
Copy link
Author

r00k commented Nov 29, 2011

Loading test environment (Rails 3.1.0)
>> Factory(:cycle)
TypeError: can't convert String into Integer
        from /Users/ben/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.0/lib/active_record/associations/belongs_to_association.rb:44:in `[]'
        from /Users/ben/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.0/lib/active_record/associations/belongs_to_association.rb:44:in `replace_keys'
        from /Users/ben/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.0/lib/active_record/associations/belongs_to_polymorphic_association.rb:13:in `replace_keys'
        from /Users/ben/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.0/lib/active_record/associations/belongs_to_association.rb:9:in `replace'
        from /Users/ben/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.0/lib/active_record/associations/singular_association.rb:17:in `writer'
        from /Users/ben/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.0/lib/active_record/associations/builder/association.rb:49:in `block in define_writers'
        from /Users/ben/.rvm/gems/ruby-1.9.3-p0/gems/factory_girl-2.1.2/lib/factory_girl/proxy/build.rb:22:in `set'
        from /Users/ben/.rvm/gems/ruby-1.9.3-p0/gems/factory_girl-2.1.2/lib/factory_girl/attribute/static.rb:14:in `add_to'
        from /Users/ben/.rvm/gems/ruby-1.9.3-p0/gems/factory_girl-2.1.2/lib/factory_girl/factory.rb:105:in `block in run'
        from /Users/ben/.rvm/gems/ruby-1.9.3-p0/gems/factory_girl-2.1.2/lib/factory_girl/attribute_list.rb:31:in `each'
        from /Users/ben/.rvm/gems/ruby-1.9.3-p0/gems/factory_girl-2.1.2/lib/factory_girl/attribute_list.rb:31:in `each'
        from /Users/ben/.rvm/gems/ruby-1.9.3-p0/gems/factory_girl-2.1.2/lib/factory_girl/factory.rb:102:in `run'
        from /Users/ben/.rvm/gems/ruby-1.9.3-p0/gems/factory_girl-2.1.2/lib/factory_girl/syntax/methods.rb:54:in `create'
        from /Users/ben/.rvm/gems/ruby-1.9.3-p0/gems/factory_girl-2.1.2/lib/factory_girl/syntax/vintage.rb:53:in `default_strategy'
        from /Users/ben/.rvm/gems/ruby-1.9.3-p0/gems/factory_girl-2.1.2/lib/factory_girl/syntax/vintage.rb:146:in `Factory'
        from (irb):1
        from /Users/ben/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.1.0/lib/rails/commands/console.rb:45:in `start'
        from /Users/ben/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.1.0/lib/rails/commands/console.rb:8:in `start'
        from /Users/ben/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.1.0/lib/rails/commands.rb:40:in `<top (required)>'
        from script/rails:6:in `require'
        from script/rails:6:in `<main>'>> 

@r00k
Copy link
Author

r00k commented Nov 29, 2011

Removing line 11 in factories.rb stops the error.

@r00k
Copy link
Author

r00k commented Nov 29, 2011

Just in case someone else finds this someday: the issue was the association in the cycle factory needed to be declared like

factory :cycle do
  user
  association :decision, factory: rental # this is the change
  effective_at { Date.beginning_of_month }
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment