Permalink
Browse files

Qualified column names work in hash conditions, like :conditions => {…

… 'comments.created_at' => ... }. Closes #9733.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7943 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent cfbd790 commit f575757ca47f2bbce9866e2a1b1f23b629352b92 @jeremy jeremy committed Oct 16, 2007
Showing with 20 additions and 1 deletion.
  1. +2 −0 activerecord/CHANGELOG
  2. +13 −1 activerecord/lib/active_record/base.rb
  3. +5 −0 activerecord/test/finder_test.rb
View
@@ -1,5 +1,7 @@
*SVN*
+* Qualified column names work in hash conditions, like :conditions => { 'comments.created_at' => ... }. #9733 [danger]
+
* Fix regression where the association would not construct new finder SQL on save causing bogus queries for "WHERE owner_id = NULL" even after owner was saved. #8713 [Bryan Helmkamp]
* Refactor association create and build so before & after callbacks behave consistently. #8854 [lifofifo, mortent]
@@ -1570,9 +1570,21 @@ def sanitize_sql_for_assignment(assignments)
# # => "status IS NULL and group_id IN (1,2,3)"
# { :age => 13..18 }
# # => "age BETWEEN 13 AND 18"
+ # { 'other_records.id' => 7 }
+ # # => "`other_records`.`id` = 7"
def sanitize_sql_hash_for_conditions(attrs)
conditions = attrs.map do |attr, value|
- "#{quoted_table_name}.#{connection.quote_column_name(attr)} #{attribute_condition(value)}"
+ 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)
+ else
+ table_name = quoted_table_name
+ end
+
+ "#{table_name}.#{connection.quote_column_name(attr)} #{attribute_condition(value)}"
end.join(' AND ')
replace_bind_variables(conditions, expand_range_bind_variables(attrs.values))
@@ -160,6 +160,11 @@ def test_find_on_hash_conditions
assert_raises(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => { :approved => true }) }
end
+ def test_find_on_hash_conditions_with_explicit_table_name
+ assert Topic.find(1, :conditions => { 'topics.approved' => false })
+ assert_raises(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => { 'topics.approved' => true }) }
+ end
+
def test_find_on_association_proxy_conditions
assert_equal [1, 2, 3, 5, 6, 7, 8, 9, 10], Comment.find_all_by_post_id(authors(:david).posts).map(&:id).sort
end

0 comments on commit f575757

Please sign in to comment.