Created
November 5, 2012 12:44
-
-
Save JonMidhir/4017019 to your computer and use it in GitHub Desktop.
Does validates :uniqueness get called even if a field has not changed?
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
# Does validates :uniqueness get called even if a field has not changed? | |
# Isn't this a performance issue? | |
validates :name, :schedule_id, :uniqueness => true | |
# My experience that it does. So isn't it almost always necessary to make sure a change has taken | |
# place before running the validation? As every field being checked for uniqueness requires a | |
# database hit. | |
# This would be better: | |
validates :name, :schedule_id, :uniqueness => true, :if => "name_changed? || schedule_id_changed?" | |
# And this much better, if a bit more verbose: | |
validates :name, :uniqueness => true, :if => :name_changed? | |
validates :schedule_id, :uniqueness => true, :if => schedule_id_changed? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment