訪問父模型我有像這樣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