Reputation: 5981
I have this kind of db:schema :
articles +-------+ article_users +------+ user
How can I restrict an user to vote more than one time on an article ?
Is there a validation rule to put on model for that ?
Upvotes: 0
Views: 40
Reputation: 11299
Begin by replacing to a has_many :through relationship if you're using has_and_belongs_to_many, so that you'll be able to access the relation model, ie. : ArticleUser.
Then first validate the relation model ArticleUser :
validates_uniqueness_of :user_id, scope => :article_id
If you want to know if a user has already voted for an article, you can use something like :
ArticleUser.exists?(:user_id => @user.id, :article_id => @article.id)
or directly using the has_many relation...
Upvotes: 2