aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorJon Leighton <j@jonathanleighton.com>2011-01-02 21:15:03 +0000
committerAaron Patterson <aaron.patterson@gmail.com>2011-01-03 16:24:32 -0800
commit31d101879f1acae604d24d831a4b82a4482acf31 (patch)
treeb9c48a53342a25a9bc622be1932f7109d4debe21 /activerecord
parent3103296a61709e808aa89c3d37cf22bcdbc5a675 (diff)
downloadrails-31d101879f1acae604d24d831a4b82a4482acf31.tar.gz
rails-31d101879f1acae604d24d831a4b82a4482acf31.tar.bz2
rails-31d101879f1acae604d24d831a4b82a4482acf31.zip
Use the association directly in other places too
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/associations/association_collection.rb7
-rw-r--r--activerecord/lib/active_record/associations/has_many_association.rb6
-rw-r--r--activerecord/lib/active_record/associations/has_one_association.rb20
3 files changed, 13 insertions, 20 deletions
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)