From 40f359b216cb7567a111f12ddee02e8b8f0f1ef3 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Sat, 15 May 2010 17:43:35 -0300 Subject: [PATCH 1/2] When use where more than once on the same column, relation doesn't do an 'or' or 'in' with the values --- activerecord/test/cases/relations_test.rb | 9 +++++++++ 1 files changed, 9 insertions(+), 0 deletions(-) diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index c9313fe..cdcbc18 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -392,6 +392,15 @@ class RelationTest < ActiveRecord::TestCase assert authors.all.blank? end + def test_find_all_using_where_twice_should_or_the_relation + david = authors(:david) + relation = Author.unscoped + relation = relation.where(:name => david.name) + relation = relation.where(:name => 'Santiago') + relation = relation.where(:id => david.id) + assert_equal [david], relation.all + end + def test_exists davids = Author.where(:name => 'David') assert davids.exists? -- 1.7.0.4 From 68fb447345ca45473e253c6f0a36f829743174e4 Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Wed, 4 Aug 2010 18:22:06 +0530 Subject: [PATCH 2/2] default_scope treats hashes and relations inconsistently when overwriting [#4598 state:committed] --- .../lib/active_record/relation/query_methods.rb | 12 ++++++++++++ 1 files changed, 12 insertions(+), 0 deletions(-) diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index a92d180..9f61bfa 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -131,18 +131,30 @@ module ActiveRecord arel = build_joins(arel, @joins_values) unless @joins_values.empty? + where_eq = {} @where_values.uniq.each do |where| next if where.blank? case where when Arel::SqlLiteral arel = arel.where(where) + when Arel::Predicates::Equality + column_name = where.operand1.name + where_eq.merge!(column_name => (where_eq.fetch(column_name, []) << where) ) else sql = where.is_a?(String) ? where : where.to_sql arel = arel.where(Arel::SqlLiteral.new("(#{sql})")) end end + where_eq.each do |key, value| + arel = if value.size > 1 + arel.where(arel[value.first.operand1.name].in(value.collect(&:operand2))) + else + arel.where(Arel::SqlLiteral.new("(#{value.to_sql})")) + end + end + arel = arel.having(*@having_values.uniq.select{|h| h.present?}) unless @having_values.empty? arel = arel.take(@limit_value) if @limit_value -- 1.7.0.4