Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed eager load error on find with include => [:table_name] and hash…

… conditions like {:table_name => {:column => 'value'}}

Signed-off-by: Michael Koziarski <michael@koziarski.com>
  • Loading branch information...
commit 026b78f9076216990bddb1aa5d83d23a647c02a5 1 parent 9e0cfdb
Anthony Crumley anthonycrumley authored NZKoz committed
20 activerecord/lib/active_record/associations.rb
View
@@ -1671,17 +1671,29 @@ def tables_in_string(string)
string.scan(/([\.a-zA-Z_]+).?\./).flatten
end
+ def tables_in_hash(hash)
+ return [] if hash.blank?
+ tables = hash.map do |key, value|
+ if value.is_a?(Hash)
+ key.to_s
+ else
+ tables_in_string(key) if key.is_a?(String)
+ end
+ end
+ tables.flatten.compact
+ end
+
def conditions_tables(options)
# look in both sets of conditions
conditions = [scope(:find, :conditions), options[:conditions]].inject([]) do |all, cond|
case cond
when nil then all
- when Array then all << cond.first
- when Hash then all << cond.keys
- else all << cond
+ when Array then all << tables_in_string(cond.first)
+ when Hash then all << tables_in_hash(cond)
+ else all << tables_in_string(cond)
end
end
- tables_in_string(conditions.join(' '))
+ conditions.flatten
end
def order_tables(options)
12 activerecord/test/cases/associations/eager_test.rb
View
@@ -223,6 +223,18 @@ def test_eager_association_loading_with_belongs_to_and_conditions_string_with_un
end
end
+ def test_eager_association_loading_with_belongs_to_and_conditions_hash
+ comments = []
+ assert_nothing_raised do
+ comments = Comment.find(:all, :include => :post, :conditions => {:posts => {:id => 4}}, :limit => 3, :order => 'comments.id')
+ end
+ assert_equal 3, comments.length
+ assert_equal [5,6,7], comments.collect { |c| c.id }
+ assert_no_queries do
+ comments.first.post
+ end
+ end
+
def test_eager_association_loading_with_belongs_to_and_conditions_string_with_quoted_table_name
quoted_posts_id= Comment.connection.quote_table_name('posts') + '.' + Comment.connection.quote_column_name('id')
assert_nothing_raised do

1 comment on commit 026b78f

Iain Hecker

+1

this one bit me recently

Please sign in to comment.
Something went wrong with that request. Please try again.