Skip to content
This repository
Browse code

Use the association directly in other places too

  • Loading branch information...
commit 31d101879f1acae604d24d831a4b82a4482acf31 1 parent 3103296
Jon Leighton authored January 02, 2011 tenderlove committed January 03, 2011
7  activerecord/lib/active_record/associations/association_collection.rb
@@ -155,14 +155,13 @@ def count(column_name = nil, options = {})
155 155
 
156 156
           @reflection.klass.count_by_sql(custom_counter_sql)
157 157
         else
158  
-
159 158
           if @reflection.options[:uniq]
160 159
             # This is needed because 'SELECT count(DISTINCT *)..' is not valid SQL.
161 160
             column_name = "#{@reflection.quoted_table_name}.#{@reflection.klass.primary_key}" unless column_name
162 161
             options.merge!(:distinct => true)
163 162
           end
164 163
 
165  
-          value = @reflection.klass.send(:with_scope, @scope) { @reflection.klass.count(column_name, options) }
  164
+          value = scoped.count(column_name, options)
166 165
 
167 166
           limit  = @reflection.options[:limit]
168 167
           offset = @reflection.options[:offset]
@@ -469,9 +468,7 @@ def create_record(attrs, &block)
469 468
           ensure_owner_is_persisted!
470 469
 
471 470
           transaction do
472  
-            with_scope(:create => @scope[:create].merge(scoped.scope_for_create)) do
473  
-              build_record(attrs, &block)
474  
-            end
  471
+            scoped.scoping { build_record(attrs, &block) }
475 472
           end
476 473
         end
477 474
 
6  activerecord/lib/active_record/associations/has_many_association.rb
@@ -26,7 +26,7 @@ def count_records
26 26
           elsif @reflection.options[:counter_sql] || @reflection.options[:finder_sql]
27 27
             @reflection.klass.count_by_sql(custom_counter_sql)
28 28
           else
29  
-            @reflection.klass.count(@scope[:find].slice(:conditions, :joins, :include))
  29
+            scoped.count
30 30
           end
31 31
 
32 32
           # If there's nothing in the database and @target has no new records
@@ -61,9 +61,7 @@ def delete_records(records)
61 61
               updates    = { @reflection.foreign_key => nil }
62 62
               conditions = { @reflection.association_primary_key => records.map { |r| r.id } }
63 63
 
64  
-              with_scope(@scope) do
65  
-                @reflection.klass.update_all(updates, conditions)
66  
-              end
  64
+              scoped.where(conditions).update_all(updates)
67 65
           end
68 66
 
69 67
           if has_cached_counter? && @reflection.options[:dependent] != :destroy
20  activerecord/lib/active_record/associations/has_one_association.rb
@@ -65,17 +65,17 @@ def replace(obj, dont_save = false)
65 65
 
66 66
       private
67 67
         def find_target
68  
-          options = @reflection.options.dup.slice(:select, :order, :include, :readonly)
69  
-
70  
-          the_target = with_scope(:find => @scope[:find]) do
71  
-            @reflection.klass.find(:first, options)
72  
-          end
73  
-          set_inverse_instance(the_target)
74  
-          the_target
  68
+          scoped.first.tap { |record| set_inverse_instance(record) }
75 69
         end
76 70
 
77 71
         def construct_find_scope
78  
-          { :conditions => construct_conditions }
  72
+          {
  73
+            :conditions => construct_conditions,
  74
+            :select     => @reflection.options[:select],
  75
+            :include    => @reflection.options[:include],
  76
+            :readonly   => @reflection.options[:readonly],
  77
+            :order      => @reflection.options[:order]
  78
+          }
79 79
         end
80 80
 
81 81
         def construct_create_scope
@@ -87,9 +87,7 @@ def new_record(replace_existing)
87 87
           # instance. Otherwise, if the target has not previously been loaded
88 88
           # elsewhere, the instance we create will get orphaned.
89 89
           load_target if replace_existing
90  
-          record = @reflection.klass.send(:with_scope, :create => @scope[:create]) do
91  
-            yield @reflection
92  
-          end
  90
+          record = scoped.scoping { yield @reflection }
93 91
 
94 92
           if replace_existing
95 93
             replace(record, true)
Something went wrong with that request. Please try again.