提交 31d10187 编写于 作者: J Jon Leighton 提交者: Aaron Patterson

Use the association directly in other places too

上级 3103296a
......@@ -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) }
end
end
......
......@@ -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
......
......@@ -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)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册