如何执行验证检查(和插入错误消息到一个错误结构),其上是HAS_ONE关联的属性。Ruby on Rails的 - 的has_many属性验证
如果“衬衫”或“裤子”发生错误,我如何访问该错误? 公司将在person.shirt.errors错误[:颜色]?
当我触发person.save,针对person.shirt验证激活?
当我触发person.save并发生person.shirt错误时,错误消息将保存在哪里?在person.shirt.errors或person.errors?
class Person < ActiveRecord::Base
has_one : shirt
has_many : pants
validates :name, :presence => true
validates_length_of :name, :minimum => 3
end
person = Person.new(:name => "JD")
person.shirt.create(:color=> "red")
person.pants.create(:type=> "jeans")
person.valid?
由于某种原因,我不能够添加注释到以前的答案,所以我写我的意见在这里。 edgarjs(或任何其他人),关于这种方式的最后一个问题。 如果错误在裤子[2] .color内,错误将出现在'person.errors.at(:shirt [2] .color)'或'person.errors.at(:shirt [2])中'或者可能两种情况? 如何获取has_many关联对象的错误消息? thx – batz107 2011-03-13 14:09:57