2011-09-20 90 views
2

我想驗證模型學生是這樣的;驗證belongs_to協會

class Student < ActiveRecord::Base 
    belongs_to :room 
end 

我要確保房間是一個有效的模型,並與我只能救一個學生只有在房間有效。

我試圖改變的關聯:

belongs_to :room, :validate => true 

但它沒有改變行爲.. API說:

:validate 
    If false, don’t validate the associated objects when saving the parent object. false by default. 

所以我改變了驗證,以房:

class Room < ActiveRecord::Base 
    has_many :students, :validate => true 
end 

但這兩個選項都不能解決我

任何想法???

回答