嵌套表单验证语句重复多次

时间:2016-07-06 00:29:44

标签: ruby-on-rails validation ruby-on-rails-4 activerecord callback

鉴于以下关联:

class Parent
  has_many :a_childs
  validates_associated :a_childs
  accepts_nested_attributes_for :a_childs

  has_many :b_childs
  validates_associated :b_childs
  accepts_nested_attributes_for :b_childs

  has_many :charges
  validates_associated :charges
  accepts_nested_attributes_for :charges

  validate :test_puts

  def test_puts
    puts "validating Parent - #{self.attribute}"
  end
end


class AChild
  belongs_to :parent

  has_many :charges
  validates_associated :charges
  accepts_nested_attributes_for :charges

  validate :test_puts

  def test_puts
    puts "validating AChild - #{self.attribute}"
  end
end

class BChild
  belongs_to :parent

  has_many :charges
  validates_associated :charges
  accepts_nested_attributes_for :charges

  validate :test_puts

  def test_puts
    puts "validating BChild - #{self.attribute}"
  end
end


class Charge
  belongs_to :parent
  belongs_to :a_child
  belongs_to :b_child

  validate :test_puts

  def test_puts
    puts "validating Charge - #{self.attribute}"
  end
end

当我运行如下所示的广告时:Parent一个AChilds,其中Charges每个BChild一个,{1} {1} Charge,最后在Parent本身上有1 Charge,如下所示:

Parent.create(
{
  "attribute" => "order",
  "a_childs_attributes"=> 
    [
      {
        "attribute"=>"a child 1",
        "charges_attributes" => 
          [
            {"attribute" => "a child 1 chg a"},
            {"attribute" => "a child 1 chg b"}
          ]
      },
      {
        "attribute"=>"a child 2",
        "charges_attributes" => 
          [
            {"attribute" => "a child 2 chg 1"},
            {"attribute" => "a child 2 chg 2"}
          ]
      }
    ]
  "b_childs_attributes"=> 
    [
      {
        "attribute"=>"b child",
        "charges_attributes" => 
          [
            {"attribute" => "b child chg"}
          ]
      },
    ]
  "charges_attributes" =>
    [
      {"attribute" => "order chg"}
    ]
}

我希望puts语句的运行方式如下:

validating Charge - a child 1 chg a
validating Charge - a child 1 chg b
validating AChild - a child 1
validating Charge - a child 2 chg 1
validating Charge - a child 2 chg 2
validating AChild - a child 2
validating Charge - b child chg
validating BChild - b child
validating Charge - order chg
validating Order - order

但我得到的是这个:

# In each validation, the object ID is NOT present, so hasn't saved to DB yet
validating Charge - a child 1 chg a
validating Charge - a child 1 chg b
validating Charge - a child 1 chg a ** repeat
validating Charge - a child 1 chg b ** repeat
validating AChild - a child 1
validating Charge - a child 2 chg 1
validating Charge - a child 2 chg 2
validating Charge - a child 2 chg 1 ** repeat
validating Charge - a child 2 chg 2 ** repeat
validating AChild - a child 2

validating Charge - a child 1 chg a ** repeat
validating Charge - a child 1 chg b ** repeat
validating Charge - a child 1 chg a ** repeat
validating Charge - a child 1 chg b ** repeat
validating AChild - a child 1       ** repeat
validating Charge - a child 2 chg 1 ** repeat
validating Charge - a child 2 chg 2 ** repeat
validating Charge - a child 2 chg 1 ** repeat
validating Charge - a child 2 chg 2 ** repeat
validating AChild - a child 2       ** repeat

validating Charge - b child chg
validating Charge - b child chg     ** repeat
validating BChild - b child

validating Charge - b child chg     ** repeat
validating Charge - b child chg     ** repeat
validating BChild - b child         ** repeat

validating Charge - order chg
validating Charge - order chg       ** repeat
validating Order - order

<then a bunch of insert happens into the database, but somehow afterwards validations repeat again??? these last round of validations are basically what I would expect as I wrote initially above>

# Note in these validations, each object's ID is defined (as in it's saved to DB)
validating Charge - a child 1 chg a
validating Charge - a child 1 chg b
validating AChild - a child 1
validating Charge - a child 2 chg 1
validating Charge - a child 2 chg 2
validating AChild - a child 2
validating Charge - b child chg
validating BChild - b child
validating Charge - order chg
validating Order - order    

导致验证重复的原因是什么,如何简化验证?

0 个答案:

没有答案