Rails嵌套属性关联验证失败

时间:2010-01-20 15:56:32

标签: ruby-on-rails validation activerecord nested associations

我有Rails模型的嵌套属性,并且关联验证由于某种原因失败。我没有使用accepts_nested_attributes_for,但我正在做一些非常相似的事情。

class Project < ActiveRecord::Base
  has_many :project_attributes

  def name
    project_attributes.find_by_name("name")
  end

  def name=(val)
    attribute = project_attributes.find_by_name("name")

    if attribute
      attribute.value = val
    else
      project_attributes.build(:name=>"name", :value=>val)
    end
  end
end

class ProjectAttribute < ActiveRecord::Base
  belongs_to :project

  validates_presence_of :name
  validates_uniqueness_of :name, :scope => :project_id

  validates_presence_of :project_id, :unless => lambda {|attribute| attribute.project.try(:valid?)}
  validates_associated :project

end

这是一个人为的例子,但与我想要做的类似。我已经看了一下accepts_nested_attributes_for做了什么,它使用了构建方法,我认为它会将构建的属性与项目相关联。

我还看了accepts_nested_attributes_for child association validation failing,它给了我validates_presence_of :unless=>valid?

有关如何使其发挥作用的任何想法?

2 个答案:

答案 0 :(得分:5)

validates_associated看起来比它的价值更麻烦。如果删除validates_presence_of:project_id,则示例有效。这是一个恢复验证的hacky示例(找到描述here)。

class ProjectAttribute < ActiveRecord::Base
  belongs_to :project

  validates_presence_of :name
  validates_uniqueness_of :name, :scope => :project_id

  validates_presence_of :project_id, :unless => Proc.new { |project_attribute|
    project = project_attribute.project
    ObjectSpace.each_object(Project) {|o| project = o if o.project_attributes.include?(project_attribute)} unless project
    project
  }
end

答案 1 :(得分:-1)

只需在project_id上添加非null约束,如果出现时髦的事情就让它爆炸。哦,并确保你的测试覆盖率也很好。