Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bakineggs/4774391 to your computer and use it in GitHub Desktop.
Save bakineggs/4774391 to your computer and use it in GitHub Desktop.
# 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