我有以下型号:
class item < ActiveRecord::Base
has_one :price
end
class price < ActiveRecord::Base
belongs_to :item
end
def
if !@item.price
@item.price.build
end
end
我明白了:
undefined method `build' for nil:NilClass
如果价格未在价格表中退出,我如何为物品建立价格?
THX
答案 0 :(得分:0)
@price = @item.price || @item.build_price
http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html