Validate uniqueness within a recent set
- by Matchu
Is there a standard Rails 3 way of detecting uniqueness within a particular scope, or is this custom validation all I can do?
class Post < ActiveRecord::Base
# [named scope for recent posts]
validates do |post|
if Post.recent.where('url = ?', post.url).count > 0
errors[:url] = 'has already been posted recently - thanks anyway!'
end
end
end
(Haven't yet tried that exact code, so there may be errors, but you get the idea.)