From 31d101879f1acae604d24d831a4b82a4482acf31 Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Sun, 2 Jan 2011 21:15:03 +0000 Subject: Use the association directly in other places too --- .../associations/association_collection.rb | 7 ++----- .../associations/has_many_association.rb | 6 ++---- .../associations/has_one_association.rb | 20 +++++++++----------- 3 files changed, 13 insertions(+), 20 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb index 8b0017e7bf..6defc465d8 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -155,14 +155,13 @@ module ActiveRecord @reflection.klass.count_by_sql(custom_counter_sql) else - if @reflection.options[:uniq] # This is needed because 'SELECT count(DISTINCT *)..' is not valid SQL. column_name = "#{@reflection.quoted_table_name}.#{@reflection.klass.primary_key}" unless column_name options.merge!(:distinct => true) end - value = @reflection.klass.send(:with_scope, @scope) { @reflection.klass.count(column_name, options) } + value = scoped.count(column_name, options) limit = @reflection.options[:limit] offset = @reflection.options[:offset] @@ -469,9 +468,7 @@ module ActiveRecord ensure_owner_is_persisted! transaction do - with_scope(:create => @scope[:create].merge(scoped.scope_for_create)) do - build_record(attrs, &block) - end + scoped.scoping { build_record(attrs, &block) } end end diff --git a/activerecord/lib/active_record/associations/has_many_association.rb b/activerecord/lib/active_record/associations/has_many_association.rb index ca02aa8612..2060f1ff62 100644 --- a/activerecord/lib/active_record/associations/has_many_association.rb +++ b/activerecord/lib/active_record/associations/has_many_association.rb @@ -26,7 +26,7 @@ module ActiveRecord elsif @reflection.options[:counter_sql] || @reflection.options[:finder_sql] @reflection.klass.count_by_sql(custom_counter_sql) else - @reflection.klass.count(@scope[:find].slice(:conditions, :joins, :include)) + scoped.count end # If there's nothing in the database and @target has no new records @@ -61,9 +61,7 @@ module ActiveRecord updates = { @reflection.foreign_key => nil } conditions = { @reflection.association_primary_key => records.map { |r| r.id } } - with_scope(@scope) do - @reflection.klass.update_all(updates, conditions) - end + scoped.where(conditions).update_all(updates) end if has_cached_counter? && @reflection.options[:dependent] != :destroy diff --git a/activerecord/lib/active_record/associations/has_one_association.rb b/activerecord/lib/active_record/associations/has_one_association.rb index 7bbeb8829a..0070606c24 100644 --- a/activerecord/lib/active_record/associations/has_one_association.rb +++ b/activerecord/lib/active_record/associations/has_one_association.rb @@ -65,17 +65,17 @@ module ActiveRecord private def find_target - options = @reflection.options.dup.slice(:select, :order, :include, :readonly) - - the_target = with_scope(:find => @scope[:find]) do - @reflection.klass.find(:first, options) - end - set_inverse_instance(the_target) - the_target + scoped.first.tap { |record| set_inverse_instance(record) } end def construct_find_scope - { :conditions => construct_conditions } + { + :conditions => construct_conditions, + :select => @reflection.options[:select], + :include => @reflection.options[:include], + :readonly => @reflection.options[:readonly], + :order => @reflection.options[:order] + } end def construct_create_scope @@ -87,9 +87,7 @@ module ActiveRecord # instance. Otherwise, if the target has not previously been loaded # elsewhere, the instance we create will get orphaned. load_target if replace_existing - record = @reflection.klass.send(:with_scope, :create => @scope[:create]) do - yield @reflection - end + record = scoped.scoping { yield @reflection } if replace_existing replace(record, true) -- cgit v1.2.3