有一個嵌套的形式中,該關係是像這樣嵌套窗體 - 如何基於子模型上的輸入驗證父模型?
class Inspection < ActiveRecord::Base
has_many :inspection_components
accepts_nested_attributes_for :inspection_components
class InspectionComponent < ActiveRecord::Base
belongs_to :inspection
我在檢查一個自定義的驗證方法,其取決於爲InspectionComponent輸入的屬性。我如何驗證 - InspectionComponent屬性在檢驗驗證中未保存或可用。
謝謝!
編輯:爲了讓事情更清楚些,下面是我想要做的一個例子。
檢查具有屬性狀態。 InspectionComponent也有一個屬性狀態。
檢查編輯表單嵌套了InspectionComponents,並且可以更新此表單上的每個模型的狀態。如果@ inspection_component.status =='complete',@ inspection.status應該只能標記爲'complete'。
因此,在驗證@inspection時,我必須能夠看到用戶爲@ inspection_component.status輸入的內容。
很明顯,我可以訪問控制器中兩個實例的參數,但是在應該進行驗證的模型中,我沒有看到實現這種情況的方法。
希望這很清楚,謝謝。
我的回答有幫助嗎? – DJTripleThreat 2010-09-11 07:54:46
這是的,但它沒有解決整個問題,請在下面的帖子下面看到我的評論。謝謝。 – 46and2 2010-09-12 06:41:55