Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fix eager loading's :condition sanitizing expanding against the wrong…

… table

Signed-off-by: Michael Koziarski <michael@koziarski.com>
[#1101 state:commited]
  • Loading branch information...
commit 7823c50c975df975ca3237bfbe9ca0b3c60f6b6d 1 parent e34e6d6
@willbryant willbryant authored NZKoz committed
View
10 activerecord/lib/active_record/association_preload.rb
@@ -96,7 +96,7 @@ def preload_has_and_belongs_to_many_association(records, reflection, preload_opt
options = reflection.options
conditions = "t0.#{reflection.primary_key_name} #{in_or_equals_for_ids(ids)}"
- conditions << append_conditions(options, preload_options)
+ conditions << append_conditions(reflection, preload_options)
associated_records = reflection.klass.find(:all, :conditions => [conditions, ids],
:include => options[:include],
@@ -233,7 +233,7 @@ def preload_belongs_to_association(records, reflection, preload_options={})
end
end
conditions = "#{table_name}.#{connection.quote_column_name(primary_key)} #{in_or_equals_for_ids(ids)}"
- conditions << append_conditions(options, preload_options)
+ conditions << append_conditions(reflection, preload_options)
associated_records = klass.find(:all, :conditions => [conditions, ids],
:include => options[:include],
:select => options[:select],
@@ -254,7 +254,7 @@ def find_associated_records(ids, reflection, preload_options)
conditions = "#{reflection.klass.quoted_table_name}.#{foreign_key} #{in_or_equals_for_ids(ids)}"
end
- conditions << append_conditions(options, preload_options)
+ conditions << append_conditions(reflection, preload_options)
reflection.klass.find(:all,
:select => (preload_options[:select] || options[:select] || "#{table_name}.*"),
@@ -270,9 +270,9 @@ def interpolate_sql_for_preload(sql)
instance_eval("%@#{sql.gsub('@', '\@')}@")
end
- def append_conditions(options, preload_options)
+ def append_conditions(reflection, preload_options)
sql = ""
- sql << " AND (#{interpolate_sql_for_preload(sanitize_sql(options[:conditions]))})" if options[:conditions]
+ sql << " AND (#{interpolate_sql_for_preload(reflection.sanitized_conditions)})" if reflection.sanitized_conditions
sql << " AND (#{sanitize_sql preload_options[:conditions]})" if preload_options[:conditions]
sql
end
View
4 activerecord/lib/active_record/reflection.rb
@@ -112,6 +112,10 @@ def ==(other_aggregation)
name == other_aggregation.name && other_aggregation.options && active_record == other_aggregation.active_record
end
+ def sanitized_conditions #:nodoc:
+ @sanitized_conditions ||= klass.send(:sanitize_sql, options[:conditions]) if options[:conditions]
+ end
+
private
def derive_class_name
name.to_s.camelize
Please sign in to comment.
Something went wrong with that request. Please try again.