Created
February 12, 2013 23:14
-
-
Save bakineggs/4774391 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This patch enables access to the records being preloaded when eager loading | |
# an association whose conditions use interpolation to call class methods. | |
module ActiveRecord::AssociationPreload::ClassMethods | |
[:has_and_belongs_to_many, :has_one, :has_many, :belongs_to].each do |type| | |
define_method "preload_#{type}_association_with_access_to_records" do |records, reflection, *args| | |
@records_to_preload_for_reflection ||= {} | |
@records_to_preload_for_reflection[reflection] = records | |
send "preload_#{type}_association_without_access_to_records", records, reflection, *args | |
@records_to_preload_for_reflection.delete reflection | |
end | |
alias_method_chain "preload_#{type}_association", :access_to_records | |
end | |
def append_conditions_with_access_to_records reflection, *args | |
@records_to_preload = @records_to_preload_for_reflection[reflection] | |
sql = append_conditions_without_access_to_records reflection, *args | |
remove_instance_variable :@records_to_preload | |
sql | |
end | |
alias_method_chain :append_conditions, :access_to_records | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment