2011-10-12 58 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?"