validating creation by post_id and ip_address - Ruby on Rails

Posted by bgadoci on Stack Overflow See other posts from Stack Overflow or by bgadoci
Published on 2010-04-17T04:52:00Z Indexed on 2010/04/17 4:53 UTC
Read the original article Hit count: 357

I have created a blog application using Ruby on Rails which includes the ability to vote on posts. A user can click vote and a record is created in the vote table. I am now trying to limit that same person from voting for a post multiple times.

class Post
 has_many :votes
end


class Vote
 belongs_to :post
end

When a vote record is created I am using the VotesController to pass the :post_id and using a hidden field in the view to pass the ip_address (both to the vote table). I am wondering if there is a way to add a validation to the Vote Model that searches to see if a post_id has an ip_address that matches the person requesting to vote.

I have tried simply using the validates_uniqueness_of :ip_address but that restricts the user from voting on any post. I just want to restrict the user from voting on a particular post that they have already voted on.

Is there a way to do this through validation?

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby