Permalink
Browse files

Define array methods in ActiveRecord::Associations::CollectionProxy

if they are not defined or delegated.
In this way, we have a performance boost invoking some array methods
which are not defined in CollectionAssociation.
  • Loading branch information...
1 parent 7ecd6a7 commit 7dc03cfd768b730933c5d4331b8202237f2fe10f @jmbejar jmbejar committed Apr 12, 2012
Showing with 13 additions and 0 deletions.
  1. +13 −0 activerecord/lib/active_record/associations/collection_proxy.rb
View
13 activerecord/lib/active_record/associations/collection_proxy.rb
@@ -126,6 +126,19 @@ def reload
proxy_association.reload
self
end
+
+ # Define array public methods because we know it should be invoked over
+ # the target, so we can have a performance improvement using those methods
+ # in association collections
+ Array.public_instance_methods.each do |m|
+ unless method_defined?(m)
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
+ def #{m}(*args, &block)
+ target.public_send(:#{m}, *args, &block) if load_target
+ end
+ RUBY
+ end
+ end
end
end
end

0 comments on commit 7dc03cf

Please sign in to comment.