Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save yuroyoro/487451bf86b108ab0191 to your computer and use it in GitHub Desktop.
Save yuroyoro/487451bf86b108ab0191 to your computer and use it in GitHub Desktop.
ActiveRecord::Associations::CollectionProxy#new の性能劣化を回復させる危険な闇patch
diff --git activerecord/lib/active_record/associations/collection_proxy.rb activerecord/lib/active_record/associations/collection_proxy.rb
index a034753..c5b84e5 100644
--- activerecord/lib/active_record/associations/collection_proxy.rb
+++ activerecord/lib/active_record/associations/collection_proxy.rb
@@ -28,13 +28,23 @@ module ActiveRecord
# is computed directly through SQL and does not trigger by itself the
# instantiation of the actual post records.
class CollectionProxy < Relation
+
delegate(*(ActiveRecord::Calculations.public_instance_methods - [:count]), to: :scope)
delegate :find_nth, to: :scope
+ delegate :merge!, to: :scope
+
+ delegate(*(VALUE_METHODS.map{|name| "#{name}_values="}), to: :scope)
+ delegate(*((VALUE_METHODS - [:create_with]).map{|name| "#{name}_values"}), to: :scope)
def initialize(klass, association) #:nodoc:
@association = association
super klass, klass.arel_table
- merge! association.scope(nullify: false)
+
+ if scope = association.reflection.scope
+ rel = association.klass.unscoped.instance_exec(association.owner, &scope)
+ self.extend(*rel.extending_values) unless rel.extending_values.blank?
+ end
+ Array.wrap(association.options[:extend]).each { |ext| proxy_extend(ext) }
end
def target
diff --git activerecord/lib/active_record/relation/merger.rb activerecord/lib/active_record/relation/merger.rb
index a27f990..23b3e27 100644
--- activerecord/lib/active_record/relation/merger.rb
+++ activerecord/lib/active_record/relation/merger.rb
@@ -121,10 +121,10 @@ module ActiveRecord
relation.where_values = where_values
relation.bind_values = bind_values
- if other.reordering_value
+ if other.reordering_value.present?
# override any order specified in the original relation
relation.reorder! other.order_values
- elsif other.order_values
+ elsif other.order_values.present?
# merge in order_values from relation
relation.order! other.order_values
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment