2012-05-18 22 views
1

父母時,我有以下逆對象禁用驗證。這意味着當你保存一個用戶時,它需要:密碼和:password_confirmation來保存。Mongoid:</p> <pre><code>def User has_and_belongs_to_many: :following, class: "User", inverse: :followers has_and_belongs_to_many: :followers, class: "User", inverse: :following end </code></pre> <p>請注意,用戶也是一個設計目標:節約HABTM關係選項

在Rails應用程序的環境中使用設計的,我有機會獲得誰是當前登錄用戶的CURRENT_USER:

following_user = User.find(following_id) 
current_user.following.push(following_user) 

「CURRENT_USER」得到,因爲它是認證保存好,但following_user確實不是因爲它失敗驗證失敗:密碼和:password_confirmation。

有無論如何,我可以禁用驗證反相對象?

我試圖在反面的兩邊附加「validate:false」,但沒有任何區別。 (在這種情況下,我是否理解了驗證選項?)

處理這種情況的建議方法是什麼? 謝謝。

回答

1

在設計驗證密碼是作爲

validates_presence_of  :password, :if => :password_required? 
validates_confirmation_of :password, :if => :password_required? 

和password_required方法是

def password_required? 
    !persisted? || !password.nil? || !password_confirmation.nil? 
end 

可以覆蓋在你的用戶模型此方法,您需要的邏輯。

相關問題