Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Use the association directly in other places too

  • Loading branch information...
commit 31d101879f1acae604d24d831a4b82a4482acf31 1 parent 3103296
@jonleighton jonleighton authored tenderlove committed
View
7 activerecord/lib/active_record/associations/association_collection.rb
@@ -155,14 +155,13 @@ def count(column_name = nil, options = {})
@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 @@ def create_record(attrs, &block)
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) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
end
end
View
6 activerecord/lib/active_record/associations/has_many_association.rb
@@ -26,7 +26,7 @@ def count_records
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 @@ def delete_records(records)
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
View
20 activerecord/lib/active_record/associations/has_one_association.rb
@@ -65,17 +65,17 @@ def replace(obj, dont_save = false)
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 @@ def new_record(replace_existing)
# 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)

0 comments on commit 31d1018

Please sign in to comment.
Something went wrong with that request. Please try again.