繁体   English   中英

Ruby on Rails - 嵌套属性:如何从子模型访问父模型

[英]Ruby on Rails - nested attributes: How do I access the parent model from child model

我有几个这样的模特

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表单中的嵌套属性中读取bill_items时,属性bill_item.bill_id或bill_item.bill在保存之前不可用。

那我该怎么做呢?

这就是我最终解决的问题; 通过在回调上设置父级

  has_many :bill_items, :before_add => :set_nest

private
  def set_nest(bill_item)
    bill_item.bill ||= self
  end

在Rails 4中(未在早期版本上测试),您可以通过在has_manyhas_one上设置inverse_of选项来访问父模型:

class Bill < ActiveRecord::Base
  has_many :bill_items, inverse_of: :bill
  belongs_to :store

  accepts_nested_attributes_for :bill_items
end

文档: 双向关联

bill_item.bill应该可用,您可以尝试提升self.bill.inspect以查看它是否存在,但我认为问题出在其他地方。

我通过在回调中设置父“来修复”它:

class Bill < ActiveRecord::Base
  has_many :bill_items, :dependent => :destroy, :before_add => :set_nest
  belongs_to :store

  accepts_nested_attributes_for :bill_items

  def set_nest(item)
    item.bill ||= self
  end
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

set_nest方法完成了这个伎俩。 希望它与accepts_nested_attributes_for一起标准化。

是的,这种问题可能很烦人。 您可以尝试将虚拟属性添加到Bill Item模型,如下所示:

class BillItem <ActiveRecord::Base
  belongs_to :product
  belongs_to :bill

  attr_accessible :store_id

  validate :has_enough_stock

  def has_enough_stock
   stock_available = Inventory.product_is(self.product).store_is(load_bill_store).one.quantity
   errors.add(:quantity, "only #{stock_available} is available") if stock_available < self.quantity
  end

  private

  def load_bill_store
    Store.find_by_id(self.store_id)
  end
end

然后在您的视图中,您可以添加如下隐藏字段:

<%= bill_item.hidden_field :store_id, :value => store_id %>

这还没有测试,但它可能会起作用。 你可能不会觉得在html中使用store_id是可取的,但它可能不是一个问题。 如果这有帮助,请告诉我。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM