访问父模型我有像这样Ruby on Rails的 - 嵌套的属性:我如何从子模型
class Bill < ActiveRecord::Base
has_many :bill_items
belongs_to :store
accepts_nested_attributes_for :bill_items
end
class BillItem <ActiveRecord::Base
belongs_to :product
belongs_to :bill
validate :has_enough_stock
def has_enough_stock
stock_available = Inventory.product_is(self.product).store_is(self.bill.store).one.quantity
errors.add(:quantity, "only #{stock_available} is available") if stock_available < self.quantity
end
end
以上验证了几个模型,是因为当我读显然是行不通的从票据表格内嵌套的属性bill_items,属性bill_item.bill_id或bill_item.bill未保存之前可用。
那我怎么去这样做类似的东西?
我解决了这个加入电话回联想,:before_add =>:set_nest – TMaYaD 2011-02-19 06:35:40