2011-10-12 150 views
2

只是想知道驗證時使用的最佳調用,除非對象具有關聯。例如:我想驗證用戶的電子郵件和密碼,除非它有一個身份驗證(OAuth)。我試過使用:Rails驗證除非關聯

# user.rb 
has_many :authentications 
validates_presence_of :email, :unless => "authentications.exists?" 
validates_presence_of :password, :unless => "authentications.exists?" 

哪些工作正常'更新',但不''創造'。

+0

默認爲'validates_presence_of'是發生吧':上=>:save',所以我不知道爲什麼你會看到這個問題...你改變了你的用戶' .create'方法?? – Fishz

回答

1

嘗試validates_presence_of :email, :unless => "authentications.present?"