【发布时间】:2014-02-09 22:30:12
【问题描述】:
我有一个模型 Indent,它有很多 indent_items 并且通过 indent_items 有很多发货。
class Indent < ActiveRecord::Base
before_destroy :check_for_shipments # WORKS HERE
has_many :indent_items, inverse_of: :indent, :dependent => :destroy
has_many :shipments, :through => :indent_items
# before_destroy :check_for_shipments # DOESN"T WORK HERE
private
def check_for_shipments
# Should not be allowed to delete if there are any shipments.
if self.shipments.count > 0
errors.add(:base, "Cannot delete indent because shipments are there.")
return false
end
end
end
我猜这可能是因为如果在关联之后提到回调,所有缩进项都被标记为删除,并且发货计数检查总是返回零。
但这不应该发生。或者我可能在这里遗漏了一些东西。我不知道。
我正在使用 Rails 3.2.8。
【问题讨论】:
-
我不认为订单应该有所作为。尝试创建一个独立的 gist 来重现错误:edgeguides.rubyonrails.org/…
-
好的。很快就会这样做。
标签: ruby-on-rails ruby-on-rails-3 activerecord ruby-on-rails-3.2