polymorphic association error ..

81 views
Skip to first unread message

kadoudal

unread,
May 15, 2012, 12:52:31 PM5/15/12
to factory_girl
I have the following models

class Partner < ActiveRecord::Base
has_many :payment_terms, :dependent => :destroy, :as => :term able

class PaymentTerm < ActiveRecord::Base
belongs_to :termable, :polymorphic => true


I defined the payment_term factory like that :

FactoryGirl.define do
factory :partner_payment_term, :class => PaymentTerm do |ppt|
ppt.sequence(:position) { |n| n }
ppt.label { Faker::Lorem.words(rand(2..4), true).join(' ') }
ppt.term_code {rand(3)}
after_build do |partner_payment_term|
partner_payment_term.termable = Factory(:independent_partner,
area: nil)
end
end
end

and the partner factory like that
FactoryGirl.define do
factory :partner do
...
trait :independent do
area
....
end
factory :independent_partner, traits: [:independent]
end



running : ppt = FactoryGirl.build(:partner_payment_term)
I get the error :

NoMethodError: undefined method `find' for
#<FactoryGirl::Declaration::Implicit:0x007f8f13d894c0>

it's linked to the after_build block, because if I comment it , then I
can build the ppt
ppt = FactoryGirl.build(:partner_payment_term )
=>
#<PaymentTerm id: nil, termable_id: nil, termable_type: nil, position:
1, label: "creator caute spero infit", rate: #<BigDecimal:
7f9322883648,'0.2E2',9(18)>, term_at: nil, term_code: 0, created_at:
nil, updated_at: nil>

what could be wrong with my after_build block ?

thansk for feedback

kadoudal

unread,
May 15, 2012, 1:11:06 PM5/15/12
to factory_girl
[SOLVED] besides some keystroke erros in this post .. sorry .. I found
the issue
the block is good .. the error was in the partner_payment_term
instance factory ... ( using an empty array in attribute definition
rather than sampling it ...
Reply all
Reply to author
Forward
0 new messages