我一直在这一段时间撞我的头。有人请复述我。工厂女孩与属性设置器的多态关联错误
方案
我有以下型号
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