Skip to content

Instantly share code, notes, and snippets.

@jonpaul
Created July 8, 2011 16:54
Show Gist options
  • Save jonpaul/1072256 to your computer and use it in GitHub Desktop.
Save jonpaul/1072256 to your computer and use it in GitHub Desktop.
validates is_good
validate :is_valid_time
def valid_time?
self.time > 24.hours.from_now
end
def is_valid_time
errors.add("Your object", "is not valid. Other explainations.") unless valid_time?
end
class Post < ActiveRecord::Base
...
validate :date_scope
private
def date_scope
if Post.where("user_id = ? AND DATE(created_at) = DATE(?)", self.user_id, Time.now).all.any?
errors.add(:user_id, "Can only post once a day")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment