如何執行驗證檢查(和插入錯誤消息到一個錯誤結構),其上是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