validates_uniqueness_of...limiting scope - How do I restrict someone from creating a certain number

Posted by bgadoci on Stack Overflow See other posts from Stack Overflow or by bgadoci
Published on 2010-05-12T04:08:28Z Indexed on 2010/05/12 4:14 UTC
Read the original article Hit count: 229

I have the following code:

class Like < ActiveRecord::Base
  belongs_to :site
  validates_uniqueness_of :ip_address, :scope => [:site_id]
end

Which limits a person from "liking" a site more than one time based on a remote ip request. Essentially when someone "likes" a site, a record is created in the Likes table and I use a hidden field to request and pass their ip address to the :ip_address column in the like table. With the above code I am limiting the user to one "like" per their ip address. I would like to limit this to a certain number for instance 10.

My initial thought was do something like this:

validates_uniqueness_of :ip_address, :scope => [:site_id, :limit => 10]

But that doesn't seem to work. Is there a simple syntax here that will allow me to do such a thing?

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby