diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index ebf1a41e85f7b27f67eb2908554d332626f5b63c..57785b4c932e3f80318d0cb818598970a730a37a 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -1463,13 +1463,6 @@ def add_touch_callbacks(reflection, touch_attribute) after_destroy(method_name) end - def find_with_associations(options, join_dependency) - rows = select_all_rows(options, join_dependency) - join_dependency.instantiate(rows) - rescue ThrowResult - [] - end - # Creates before_destroy callback methods that nullify, delete or destroy # has_many associated objects, according to the defined :dependent rule. # @@ -1693,66 +1686,6 @@ def create_has_and_belongs_to_many_reflection(association_id, options, &extensio reflection end - def select_all_rows(options, join_dependency) - connection.select_all( - construct_finder_sql_with_included_associations(options, join_dependency), - "#{name} Load Including Associations" - ) - end - - def construct_finder_arel_with_included_associations(options, join_dependency) - relation = scoped - - for association in join_dependency.join_associations - relation = association.join_relation(relation) - end - - relation = relation.apply_finder_options(options).select(column_aliases(join_dependency)) - - if !using_limitable_reflections?(join_dependency.reflections) && relation.limit_value - relation = relation.where(construct_arel_limited_ids_condition(options, join_dependency)) - end - - relation = relation.except(:limit, :offset) unless using_limitable_reflections?(join_dependency.reflections) - - relation - end - - def construct_finder_sql_with_included_associations(options, join_dependency) - construct_finder_arel_with_included_associations(options, join_dependency).to_sql - end - - def construct_arel_limited_ids_condition(options, join_dependency) - if (ids_array = select_limited_ids_array(options, join_dependency)).empty? - raise ThrowResult - else - Arel::Predicates::In.new( - Arel::SqlLiteral.new("#{connection.quote_table_name table_name}.#{primary_key}"), - ids_array - ) - end - end - - def select_limited_ids_array(options, join_dependency) - connection.select_all( - construct_finder_sql_for_association_limiting(options, join_dependency), - "#{name} Load IDs For Limited Eager Loading" - ).collect { |row| row[primary_key] } - end - - def construct_finder_sql_for_association_limiting(options, join_dependency) - relation = scoped - - for association in join_dependency.join_associations - relation = association.join_relation(relation) - end - - relation = relation.apply_finder_options(options).except(:select) - relation = relation.select(connection.distinct("#{connection.quote_table_name table_name}.#{primary_key}", relation.order_values.join(", "))) - - relation.to_sql - end - def using_limitable_reflections?(reflections) reflections.collect(&:collection?).length.zero? end diff --git a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb index 1bce45865f54b1ed3558739570c05152d591b24c..004d0156e1b14da1f71665510e7854378d820c7b 100644 --- a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb @@ -732,21 +732,6 @@ def test_assign_ids_ignoring_blanks assert_equal [projects(:active_record), projects(:action_controller)].map(&:id).sort, developer.project_ids.sort end - def test_select_limited_ids_array - # Set timestamps - Developer.transaction do - Developer.find(:all, :order => 'id').each_with_index do |record, i| - record.update_attributes(:created_at => 5.years.ago + (i * 5.minutes)) - end - end - - join_base = ActiveRecord::Associations::ClassMethods::JoinDependency::JoinBase.new(Project) - join_dep = ActiveRecord::Associations::ClassMethods::JoinDependency.new(join_base, :developers, nil) - projects = Project.send(:select_limited_ids_array, {:order => 'developers.created_at'}, join_dep) - assert !projects.include?("'"), projects - assert_equal ["1", "2"], projects.sort - end - def test_scoped_find_on_through_association_doesnt_return_read_only_records tag = Post.find(1).tags.find_by_name("General")