Flowdock
method

construct_finder_sql_for_association_limiting

Importance_0
v2.1.0 - Show latest stable - 0 notes - Class: ActiveRecord::Associations::ClassMethods
construct_finder_sql_for_association_limiting(options, join_dependency) private

No documentation

This method has no description. You can help the Ruby on Rails community by adding new notes.

Hide source
# File activerecord/lib/active_record/associations.rb, line 1458
        def construct_finder_sql_for_association_limiting(options, join_dependency)
          scope       = scope(:find)

          # Only join tables referenced in order or conditions since this is particularly slow on the pre-query.
          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 "
          if is_distinct
            sql << connection.distinct("#{connection.quote_table_name table_name}.#{primary_key}", options[:order])
          else
            sql << primary_key
          end
          sql << " FROM #{connection.quote_table_name table_name} "

          if is_distinct
            sql << distinct_join_associations.collect(&:association_join).join
            add_joins!(sql, options, scope)
          end

          add_conditions!(sql, options[:conditions], scope)
          add_group!(sql, options[:group], scope)

          if options[:order] && is_distinct
            connection.add_order_by_for_association_limiting!(sql, options)
          else
            add_order!(sql, options[:order], scope)
          end

          add_limit!(sql, options, scope)

          return sanitize_sql(sql)
        end
Register or log in to add new notes.