aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authorPratik Naik <pratiknaik@gmail.com>2010-01-02 03:46:08 +0530
committerPratik Naik <pratiknaik@gmail.com>2010-01-02 03:46:08 +0530
commit4a7a14b0e15e07dbc4e49cfdd0694c17f81cfad0 (patch)
tree104116c1b7ca12271f2318894787146d8b9cfea3 /activerecord/lib/active_record
parent498fddc714f66d5f6f9152910853ce355f059397 (diff)
downloadrails-4a7a14b0e15e07dbc4e49cfdd0694c17f81cfad0.tar.gz
rails-4a7a14b0e15e07dbc4e49cfdd0694c17f81cfad0.tar.bz2
rails-4a7a14b0e15e07dbc4e49cfdd0694c17f81cfad0.zip
Use relations to build uniqueness conditions
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r--activerecord/lib/active_record/validations/uniqueness.rb17
1 files changed, 9 insertions, 8 deletions
diff --git a/activerecord/lib/active_record/validations/uniqueness.rb b/activerecord/lib/active_record/validations/uniqueness.rb
index ffbe1b5c40..7efd312357 100644
--- a/activerecord/lib/active_record/validations/uniqueness.rb
+++ b/activerecord/lib/active_record/validations/uniqueness.rb
@@ -8,24 +8,25 @@ module ActiveRecord
def validate_each(record, attribute, value)
finder_class = find_finder_class_for(record)
+ table = finder_class.active_relation
+
table_name = record.class.quoted_table_name
sql, params = mount_sql_and_params(finder_class, table_name, attribute, value)
+ relation = table.where(sql, *params)
+
Array(options[:scope]).each do |scope_item|
scope_value = record.send(scope_item)
- sql << " AND " << record.class.send(:attribute_condition, "#{table_name}.#{scope_item}", scope_value)
- params << scope_value
+ relation = relation.where(scope_item => scope_value)
end
unless record.new_record?
- sql << " AND #{record.class.quoted_table_name}.#{record.class.primary_key} <> ?"
- params << record.send(:id)
+ # TODO : This should be in Arel
+ relation = relation.where("#{record.class.quoted_table_name}.#{record.class.primary_key} <> ?", record.send(:id))
end
- finder_class.send(:with_exclusive_scope) do
- if finder_class.exists?([sql, *params])
- record.errors.add(attribute, :taken, :default => options[:message], :value => value)
- end
+ if relation.exists?
+ record.errors.add(attribute, :taken, :default => options[:message], :value => value)
end
end