2012-06-30 46 views
1

我有一個模型設定了,象這樣:Rails的模型驗證:兩個用戶不能在同一

class Rating 
    # user_id, author_id 
end 

我想要做的就是驗證AUTHOR_ID/USER_ID這樣他們就可以不一樣,本質上,所以用戶不能評分自己。

我是否有權說這應該使用評級課程中的驗證來完成?

validates :author_id, # custom validation options 

回答

4

你需要一個自定義的驗證:

class Rating 
    # user_id, author_id 
    validate :ensure_author_is_not_user 

private 

    def ensure_author_is_not_user 
    errors[:author_id] << "can not be the same as user" unless user_id != author_id 
    end 

end 
+0

感謝。我會嘗試的。 –