From 4a7a14b0e15e07dbc4e49cfdd0694c17f81cfad0 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Sat, 2 Jan 2010 03:46:08 +0530 Subject: Use relations to build uniqueness conditions --- .../lib/active_record/validations/uniqueness.rb | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'activerecord/lib/active_record') 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 -- cgit v1.2.3