Permalink
Browse files

refactoring of uniqueness validate_each

get scope_value only one time dependig on reflection
  • Loading branch information...
1 parent ae61c03 commit 08927cf1f2ebf7425418f4ee97a3dfb80abe978e @acapilleri acapilleri committed Oct 14, 2012
Showing with 2 additions and 1 deletion.
  1. +2 −1 activerecord/lib/active_record/validations/uniqueness.rb
@@ -26,11 +26,12 @@ def validate_each(record, attribute, value)
relation = relation.and(table[finder_class.primary_key.to_sym].not_eq(record.send(:id))) if record.persisted?
Array(options[:scope]).each do |scope_item|
- scope_value = record.read_attribute(scope_item)
reflection = record.class.reflect_on_association(scope_item)
if reflection
scope_value = record.send(reflection.foreign_key)
scope_item = reflection.foreign_key
+ else
+ scope_value = record.read_attribute(scope_item)
end
relation = relation.and(table[scope_item].eq(scope_value))
end

0 comments on commit 08927cf

Please sign in to comment.