1

我正在使用 Mongoid 和嵌入式文档。我正在使用标准方法来使用_destroy值为 的隐藏表单字段1。这工作正常,除非运行包含我要删除的关联的验证器。例如:

class Thing
  include Mongoid::Document
  embeds_many :actions
  validate :uniqueness_of_actions

  def uniqueness_of_actions
    subjects = actions.map(&:subject)
    subjects_are_unique = subjects == subjects.uniq
    errors.add(:actions, 'must have unique subjects') unless subjects_are_unique
    subjects_are_unique
  end
end

在运行可能包括它们的验证之前删除/排除标记为销毁的关联的正确方法是什么?

4

1 回答 1

1

沿着这些思路尝试一些东西。

class Artist < AR::Base
  has_many :songs

  validate :custom_thing

  def custom_thing
    songs.reject{ |x| x._destroy}.each do |a|
      # magic here
    end
  end
end
于 2013-07-18T00:42:08.153 回答