mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Make sure needed table joins are included :select option. [#110 state:resolved]
Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
This commit is contained in:
parent
6df995bbf2
commit
b28b54cab0
2 changed files with 21 additions and 4 deletions
|
@ -1500,6 +1500,12 @@ module ActiveRecord
|
|||
order.scan(/([\.\w]+).?\./).flatten
|
||||
end
|
||||
|
||||
def selects_tables(options)
|
||||
select = options[:select]
|
||||
return [] unless select && select.is_a?(String)
|
||||
select.scan(/"?([\.\w]+)"?.?\./).flatten
|
||||
end
|
||||
|
||||
# Checks if the conditions reference a table other than the current model table
|
||||
def include_eager_conditions?(options,tables = nil)
|
||||
tables = conditions_tables(options)
|
||||
|
@ -1518,8 +1524,16 @@ module ActiveRecord
|
|||
end
|
||||
end
|
||||
|
||||
def include_eager_select?(options)
|
||||
selects = selects_tables(options)
|
||||
return false unless selects.any?
|
||||
selects.any? do |select|
|
||||
select != table_name
|
||||
end
|
||||
end
|
||||
|
||||
def references_eager_loaded_tables?(options)
|
||||
include_eager_order?(options) || include_eager_conditions?(options)
|
||||
include_eager_order?(options) || include_eager_conditions?(options) || include_eager_select?(options)
|
||||
end
|
||||
|
||||
def using_limitable_reflections?(reflections)
|
||||
|
|
|
@ -859,12 +859,15 @@ class FinderTest < ActiveRecord::TestCase
|
|||
end
|
||||
|
||||
def test_find_with_order_on_included_associations_with_construct_finder_sql_for_association_limiting_and_is_distinct
|
||||
assert_equal 2, Post.find(:all,:include=>{:authors=>:author_address},:order=>' author_addresses.id DESC ', :limit=>2).size
|
||||
assert_equal 2, Post.find(:all, :include => { :authors => :author_address }, :order => ' author_addresses.id DESC ', :limit => 2).size
|
||||
|
||||
assert_equal 3, Post.find(:all,:include=>{:author=>:author_address,:authors=>:author_address},
|
||||
:order=>' author_addresses_authors.id DESC ', :limit=>3).size
|
||||
assert_equal 3, Post.find(:all, :include => { :author => :author_address, :authors => :author_address},
|
||||
:order => ' author_addresses_authors.id DESC ', :limit => 3).size
|
||||
end
|
||||
|
||||
def test_with_limiting_with_custom_select
|
||||
assert_equal 3, Post.find(:all, :include => :author, :select => ' posts.*, authors.id as "author_id"', :limit => 3).size
|
||||
end
|
||||
|
||||
protected
|
||||
def bind(statement, *vars)
|
||||
|
|
Loading…
Reference in a new issue