aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2008-03-23 05:00:25 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2008-03-23 05:00:25 +0000
commit0ff031992f1dc36cf36eaf26a1e476f67c29c048 (patch)
tree6be43ca85183d7b332e6f9627ad68394495e734b /activerecord/lib
parente1790efb85010c5e6f39eef029abf7b71ae3b9a6 (diff)
downloadrails-0ff031992f1dc36cf36eaf26a1e476f67c29c048.tar.gz
rails-0ff031992f1dc36cf36eaf26a1e476f67c29c048.tar.bz2
rails-0ff031992f1dc36cf36eaf26a1e476f67c29c048.zip
Fix merging blank conditions. Closes #10764 [mcmire, cavalle]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9082 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib')
-rwxr-xr-xactiverecord/lib/active_record/base.rb29
1 files changed, 21 insertions, 8 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index 4531c12110..9716473a4e 100755
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -1426,6 +1426,20 @@ module ActiveRecord #:nodoc:
(safe_to_array(first) + safe_to_array(second)).uniq
end
+ # Merges conditions so that the result is a valid +condition+
+ def merge_conditions(*conditions)
+ segments = []
+
+ conditions.each do |condition|
+ unless condition.blank?
+ sql = sanitize_sql(condition)
+ segments << sql unless sql.blank?
+ end
+ end
+
+ "(#{segments.join(') AND (')})" unless segments.empty?
+ end
+
# Object#to_a is deprecated, though it does have the desired behavior
def safe_to_array(o)
case o
@@ -1498,12 +1512,11 @@ module ActiveRecord #:nodoc:
# The optional scope argument is for the current :find scope.
def add_conditions!(sql, conditions, scope = :auto)
scope = scope(:find) if :auto == scope
- segments = []
- segments << sanitize_sql(scope[:conditions]) if scope && !scope[:conditions].blank?
- segments << sanitize_sql(conditions) unless conditions.blank?
- segments << type_condition if finder_needs_type_condition?
- segments.delete_if{|s| s.blank?}
- sql << "WHERE (#{segments.join(") AND (")}) " unless segments.empty?
+ conditions = [conditions]
+ conditions << scope[:conditions] if scope
+ conditions << type_condition if finder_needs_type_condition?
+ merged_conditions = merge_conditions(*conditions)
+ sql << "WHERE #{merged_conditions} " unless merged_conditions.blank?
end
def type_condition
@@ -1745,7 +1758,7 @@ module ActiveRecord #:nodoc:
(hash[method].keys + params.keys).uniq.each do |key|
merge = hash[method][key] && params[key] # merge if both scopes have the same key
if key == :conditions && merge
- hash[method][key] = [params[key], hash[method][key]].collect{ |sql| "( %s )" % sanitize_sql(sql) }.join(" AND ")
+ hash[method][key] = merge_conditions(params[key], hash[method][key])
elsif key == :include && merge
hash[method][key] = merge_includes(hash[method][key], params[key]).uniq
else
@@ -1939,7 +1952,7 @@ module ActiveRecord #:nodoc:
# { :status => nil, :group_id => 1 }
# # => "status = NULL , group_id = 1"
def sanitize_sql_hash_for_assignment(attrs)
- conditions = attrs.map do |attr, value|
+ attrs.map do |attr, value|
"#{connection.quote_column_name(attr)} = #{quote_bound_value(value)}"
end.join(', ')
end