Created
July 8, 2011 16:54
-
-
Save jonpaul/1072256 to your computer and use it in GitHub Desktop.
validates is_good
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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