我有两个模型Product
和Status
在产品模型中,我有
class Product < ActiveRecord::Base
attr_accessible :image_url, :name, :status_id
belongs_to :status
state_machine :status_id, :initial => :dead do
event :activate do
transition all => :active
end
event :de_activate do
transition all => :dead
end
event :set_out_of_stock do
transition all => :out_of_stock
end
state :active, :value => Status.where(:code=>"ACTIVE").first.id
state :dead, :value => Status.where(:code=>"DEAD").first.id
state :out_of_stock, :value => Status.where(:code=>"OUT_OF_STOCK").first.id
end
end
在运行 rspec 时,它尝试首先加载产品模型并给出错误说nil.id
是 4 等....因为未填充状态。
如何在加载模型之前预加载状态数据?