From 27c81383ae7ae0d545632c6750202fe40eff9a94 Mon Sep 17 00:00:00 2001 From: Joel Chippindale Date: Tue, 16 Jun 2009 09:59:11 +0100 Subject: [PATCH] Fix hash conditions with mix of implicit and explicit table names Ensure that dot notation keys in conditions hash don't leak their table_name into subsequent conditions. --- activerecord/lib/active_record/base.rb | 9 +++++---- activerecord/test/cases/finder_test.rb | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 97c36a6..1040738 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -2305,18 +2305,19 @@ module ActiveRecord #:nodoc: # # => "address_street='123 abc st.' and address_city='chicago'" def sanitize_sql_hash_for_conditions(attrs, table_name = quoted_table_name) attrs = expand_hash_conditions_for_aggregates(attrs) - conditions = attrs.map do |attr, value| unless value.is_a?(Hash) attr = attr.to_s # Extract table name from qualified attribute names. if attr.include?('.') - table_name, attr = attr.split('.', 2) - table_name = connection.quote_table_name(table_name) + table_name_for_condition, attr = attr.split('.', 2) + table_name_for_condition = connection.quote_table_name(table_name_for_condition) + else + table_name_for_condition = table_name end - attribute_condition("#{table_name}.#{connection.quote_column_name(attr)}", value) + attribute_condition("#{table_name_for_condition}.#{connection.quote_column_name(attr)}", value) else sanitize_sql_hash_for_conditions(value, connection.quote_table_name(attr.to_s)) end diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb index 28eb311..614cc6e 100644 --- a/activerecord/test/cases/finder_test.rb +++ b/activerecord/test/cases/finder_test.rb @@ -280,6 +280,20 @@ class FinderTest < ActiveRecord::TestCase assert_raise(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => { 'topics.approved' => true }) } end + def test_find_on_hash_conditions_with_mix_of_implicit_and_explicit_table_names + # Using ordered hash for conditions because the bug this test was added for depended on order keys were processed + # see https://rails.lighthouseapp.com/projects/8994/tickets/2462-sanitize_sql_hash_for_conditions-uses-incorrect-table_name + conditions = ActiveSupport::OrderedHash.new + conditions['authors.name'] = 'David' + conditions['title'] = 'Welcome to the weblog' + + # Need to stub this because otherwise it converts the order hash to a standard hash + Post.stubs(:expand_hash_conditions_for_aggregates).returns(conditions) + assert_equal [posts(:welcome)], Post.find(:all, :include => :author, :conditions => conditions) + end + + + def test_find_on_hash_conditions_with_hashed_table_name assert Topic.find(1, :conditions => {:topics => { :approved => false }}) assert_raise(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => {:topics => { :approved => true }}) } -- 1.6.3.1