我一直在這一段時間撞我的頭。有人請複述我。工廠女孩與屬性設置器的多態關聯錯誤
方案
我有以下型號
class House < ActiveRecord::Base
has_one :tenancy, :dependent => :destroy, :as => :tenant
end
class LeaseAgreement < ActiveRecord::Base
has_many :tenancies
end
class Tenancy < ActiveRecord::Base
belongs_to :tenant, :polymorphic => true
belongs_to :lease_agreement
def lease=(lease)
if lease.rent_amount > 10000
# do something here
else
# do something else here
end
self.lease_agreement = lease
end
end
我的工廠
Factory.define :lease_agreement do |l|
l.name "Foo"
l.rent_amount 5000
end
Factory.define :tenancy do |t|
t.name "Foo"
t.association :tenant, :factory => :house
t.after_build { |tenancy| tenancy.lease = Factory.create(:lease_agreement) }
end
也試過這種
Factory.define :tenancy do |t|
t.name "Foo"
t.association :tenant, :factory => :house
t.after_build { |tenancy| tenancy.lease = Factory.create(:lease_agreement, :tenant => tenancy) }
end
當我嘗試這種方式時,在我的規範中測試的兩種方式; @house = Factory(:house)
我收到以下錯誤
NoMethodError: undefined method `rent_amount' for nil:NilClass
from /home/kibet/.rvm/gems/ruby-1.8.7-p352/gems/activesupport-3.0.5/lib/active_support/whiny_nil.rb:48:in `method_missing'
from /home/kibet/code/ruby/stuff/app/models/tenancy.rb:44:in `lease='
我該怎麼辦呢?
你家的工廠在哪裏? – Nitrodist 2012-03-12 19:13:14