From 47e608394cd611c8f901b6d7b4f3d4a8d1786d1e Mon Sep 17 00:00:00 2001 From: Tristan Dunn Date: Sun, 9 Aug 2009 15:13:44 -0400 Subject: [PATCH] Prevent overwriting of table name in merging SQL conditions. --- activerecord/lib/active_record/base.rb | 4 +++- activerecord/test/cases/named_scope_test.rb | 6 ++++++ activerecord/test/models/comment.rb | 6 +++++- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 531a698..f5e2f77 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -2294,10 +2294,12 @@ module ActiveRecord #:nodoc: # And for value objects on a composed_of relationship: # { :address => Address.new("123 abc st.", "chicago") } # # => "address_street='123 abc st.' and address_city='chicago'" - def sanitize_sql_hash_for_conditions(attrs, table_name = quoted_table_name) + def sanitize_sql_hash_for_conditions(attrs, default_table_name = quoted_table_name) attrs = expand_hash_conditions_for_aggregates(attrs) conditions = attrs.map do |attr, value| + table_name = default_table_name + unless value.is_a?(Hash) attr = attr.to_s diff --git a/activerecord/test/cases/named_scope_test.rb b/activerecord/test/cases/named_scope_test.rb index 2a729f0..694f70f 100644 --- a/activerecord/test/cases/named_scope_test.rb +++ b/activerecord/test/cases/named_scope_test.rb @@ -323,6 +323,12 @@ class NamedScopeTest < ActiveRecord::TestCase end end + def test_table_names_for_chaining_scopes_with_and_without_table_name_included + assert_nothing_raised do + Comment.for_first_post.for_first_author.all + end + end + def test_chaining_with_duplicate_joins join = "INNER JOIN comments ON comments.post_id = posts.id" post = Post.find(1) diff --git a/activerecord/test/models/comment.rb b/activerecord/test/models/comment.rb index f7f07c1..399dea9 100644 --- a/activerecord/test/models/comment.rb +++ b/activerecord/test/models/comment.rb @@ -1,6 +1,10 @@ class Comment < ActiveRecord::Base named_scope :containing_the_letter_e, :conditions => "comments.body LIKE '%e%'" - + named_scope :for_first_post, :conditions => { :post_id => 1 } + named_scope :for_first_author, + :joins => :post, + :conditions => { "posts.author_id" => 1 } + belongs_to :post, :counter_cache => true def self.what_are_you -- 1.6.3.3