aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorJohn Devine <johnjdevine@gmail.com>2008-05-03 22:49:18 -0500
committerMichael Koziarski <michael@koziarski.com>2008-05-06 21:47:10 +1200
commit8ded457b1b31b157d6fe89b553749579e5ac4a27 (patch)
tree9c9c4689ad1f9efe952b737bbf45eb9455237901 /activerecord/lib
parentfbebdb0c091c37b0bc75ab774d187d8bc8795bd2 (diff)
downloadrails-8ded457b1b31b157d6fe89b553749579e5ac4a27.tar.gz
rails-8ded457b1b31b157d6fe89b553749579e5ac4a27.tar.bz2
rails-8ded457b1b31b157d6fe89b553749579e5ac4a27.zip
Added logic to associations.rb to make sure select_for_limited_ids
includes joins that are needed to reach tables listed in the :order or :conditions options if they are not joined directly to the main active_record table. Signed-off-by: Michael Koziarski <michael@koziarski.com> [#109 state:resolved]
Diffstat (limited to 'activerecord/lib')
-rw-r--r--[-rwxr-xr-x]activerecord/lib/active_record/associations.rb25
1 files changed, 24 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index 7d27b0607a..0809b271d7 100755..100644
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -1446,6 +1446,12 @@ module ActiveRecord
tables_from_conditions = conditions_tables(options)
tables_from_order = order_tables(options)
all_tables = tables_from_conditions + tables_from_order
+ distinct_join_associations = all_tables.uniq.map{|table|
+ join_dependency.joins_for_table_name(table)
+ }.flatten.compact.uniq
+
+
+
is_distinct = !options[:joins].blank? || include_eager_conditions?(options, tables_from_conditions) || include_eager_order?(options, tables_from_order)
sql = "SELECT "
@@ -1457,7 +1463,7 @@ module ActiveRecord
sql << " FROM #{connection.quote_table_name table_name} "
if is_distinct
- sql << join_dependency.join_associations.reject{ |ja| !all_tables.include?(ja.table_name) }.collect(&:association_join).join
+ sql << distinct_join_associations.collect(&:association_join).join
add_joins!(sql, options, scope)
end
@@ -1617,6 +1623,23 @@ module ActiveRecord
end
end
+ def join_for_table_name(table_name)
+ @joins.select{|j|j.aliased_table_name == table_name.gsub(/^\"(.*)\"$/){$1} }.first rescue nil
+ end
+
+ def joins_for_table_name(table_name)
+ join = join_for_table_name(table_name)
+ result = nil
+ if join && join.is_a?(JoinAssociation)
+ result = [join]
+ if join.parent && join.parent.is_a?(JoinAssociation)
+ result = joins_for_table_name(join.parent.aliased_table_name) +
+ result
+ end
+ end
+ result
+ end
+
protected
def build(associations, parent = nil)
parent ||= @joins.last