### Home Classes Methods

### In Files

- lib/active_record/relation/query_methods.rb

### Methods

- #arel
- #bind
- #build_arel
- #create_with
- #eager_load
- #extending
- #from
- #group
- #having
- #includes
- #joins
- #limit
- #lock
- #offset
- #order
- #preload
- #readonly
- #reorder
- #reverse_order
- #select
- #uniq
- #where

### Files

- README.rdoc

### Class/Module Index

Quicksearch

- ActiveRecord
- ActiveRecord::ActiveRecordError
- ActiveRecord::AdapterNotFound
- ActiveRecord::AdapterNotSpecified
- ActiveRecord::Aggregations
- ActiveRecord::Aggregations::ClassMethods
- ActiveRecord::AssociationTypeMismatch
- ActiveRecord::Associations
- ActiveRecord::Associations::ClassMethods
- ActiveRecord::AttributeAssignment
- ActiveRecord::AttributeAssignment::ClassMethods
- ActiveRecord::AttributeAssignmentError
- ActiveRecord::AttributeMethods
- ActiveRecord::AttributeMethods::BeforeTypeCast
- ActiveRecord::AttributeMethods::ClassMethods
- ActiveRecord::AttributeMethods::DeprecatedUnderscoreRead
- ActiveRecord::AttributeMethods::DeprecatedUnderscoreRead::ClassMethods
- ActiveRecord::AttributeMethods::Dirty
- ActiveRecord::AttributeMethods::PrimaryKey
- ActiveRecord::AttributeMethods::PrimaryKey::ClassMethods
- ActiveRecord::AttributeMethods::Query
- ActiveRecord::AttributeMethods::Read
- ActiveRecord::AttributeMethods::Read::ClassMethods
- ActiveRecord::AttributeMethods::Serialization
- ActiveRecord::AttributeMethods::Serialization::Attribute
- ActiveRecord::AttributeMethods::Serialization::ClassMethods
- ActiveRecord::AttributeMethods::TimeZoneConversion
- ActiveRecord::AttributeMethods::TimeZoneConversion::ClassMethods
- ActiveRecord::AttributeMethods::Write
- ActiveRecord::AttributeMethods::Write::ClassMethods
- ActiveRecord::AutosaveAssociation
- ActiveRecord::AutosaveAssociation::ClassMethods
- ActiveRecord::Base
- ActiveRecord::Batches
- ActiveRecord::Calculations
- ActiveRecord::Callbacks
- ActiveRecord::Coders
- ActiveRecord::ConfigurationError
- ActiveRecord::ConnectionAdapters
- ActiveRecord::ConnectionAdapters::AbstractAdapter
- ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter
- ActiveRecord::ConnectionAdapters::ConnectionHandler
- ActiveRecord::ConnectionAdapters::ConnectionManagement
- ActiveRecord::ConnectionAdapters::ConnectionPool
- ActiveRecord::ConnectionAdapters::DatabaseLimits
- ActiveRecord::ConnectionAdapters::DatabaseStatements
- ActiveRecord::ConnectionAdapters::Mysql2Adapter
- ActiveRecord::ConnectionAdapters::MysqlAdapter
- ActiveRecord::ConnectionAdapters::MysqlAdapter::StatementPool
- ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
- ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::StatementPool
- ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::TableDefinition
- ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::Utils
- ActiveRecord::ConnectionAdapters::PostgreSQLColumn
- ActiveRecord::ConnectionAdapters::QueryCache
- ActiveRecord::ConnectionAdapters::Quoting
- ActiveRecord::ConnectionAdapters::SQLiteAdapter
- ActiveRecord::ConnectionAdapters::SQLiteAdapter::ExplainPrettyPrinter
- ActiveRecord::ConnectionAdapters::SQLiteAdapter::StatementPool
- ActiveRecord::ConnectionAdapters::SQLiteAdapter::Version
- ActiveRecord::ConnectionAdapters::SchemaCache
- ActiveRecord::ConnectionAdapters::SchemaStatements
- ActiveRecord::ConnectionAdapters::StatementPool
- ActiveRecord::ConnectionAdapters::Table
- ActiveRecord::ConnectionAdapters::TableDefinition
- ActiveRecord::ConnectionNotEstablished
- ActiveRecord::ConnectionTimeoutError
- ActiveRecord::CounterCache
- ActiveRecord::DangerousAttributeError
- ActiveRecord::Delegation
- ActiveRecord::DynamicFinderMatch
- ActiveRecord::DynamicMatchers
- ActiveRecord::DynamicScopeMatch
- ActiveRecord::Explain
- ActiveRecord::FinderMethods
- ActiveRecord::Fixtures
- ActiveRecord::Fixtures::File
- ActiveRecord::Generators
- ActiveRecord::Generators::Migration
- ActiveRecord::Generators::MigrationGenerator
- ActiveRecord::Generators::ModelGenerator
- ActiveRecord::Generators::ObserverGenerator
- ActiveRecord::Generators::SessionMigrationGenerator
- ActiveRecord::IdentityMap
- ActiveRecord::IdentityMap::Middleware
- ActiveRecord::Inheritance
- ActiveRecord::Inheritance::ClassMethods
- ActiveRecord::Integration
- ActiveRecord::InvalidForeignKey
- ActiveRecord::IrreversibleMigration
- ActiveRecord::Locking
- ActiveRecord::Locking::Optimistic
- ActiveRecord::Locking::Optimistic::ClassMethods
- ActiveRecord::Locking::Pessimistic
- ActiveRecord::LogSubscriber
- ActiveRecord::Migration
- ActiveRecord::Migration::CommandRecorder
- ActiveRecord::MigrationProxy
- ActiveRecord::ModelSchema
- ActiveRecord::ModelSchema::ClassMethods
- ActiveRecord::MultiparameterAssignmentErrors
- ActiveRecord::NestedAttributes
- ActiveRecord::NestedAttributes::ClassMethods
- ActiveRecord::NestedAttributes::TooManyRecords
- ActiveRecord::Observer
- ActiveRecord::Persistence
- ActiveRecord::Persistence::ClassMethods
- ActiveRecord::PreparedStatementInvalid
- ActiveRecord::QueryCache
- ActiveRecord::QueryCache::ClassMethods
- ActiveRecord::QueryMethods
- ActiveRecord::Querying
- ActiveRecord::Railtie
- ActiveRecord::Railties
- ActiveRecord::Railties::ControllerRuntime
- ActiveRecord::Railties::ControllerRuntime::ClassMethods
- ActiveRecord::ReadOnlyRecord
- ActiveRecord::ReadonlyAttributes
- ActiveRecord::ReadonlyAttributes::ClassMethods
- ActiveRecord::RecordInvalid
- ActiveRecord::RecordNotFound
- ActiveRecord::RecordNotSaved
- ActiveRecord::RecordNotUnique
- ActiveRecord::Reflection
- ActiveRecord::Reflection::ClassMethods
- ActiveRecord::Reflection::MacroReflection
- ActiveRecord::Result
- ActiveRecord::Rollback
- ActiveRecord::Sanitization
- ActiveRecord::Sanitization::ClassMethods
- ActiveRecord::Schema
- ActiveRecord::Scoping
- ActiveRecord::Scoping::ClassMethods
- ActiveRecord::Scoping::Default
- ActiveRecord::Scoping::Default::ClassMethods
- ActiveRecord::Scoping::Named
- ActiveRecord::Scoping::Named::ClassMethods
- ActiveRecord::Serialization
- ActiveRecord::SerializationTypeMismatch
- ActiveRecord::SessionStore
- ActiveRecord::SessionStore::Session
- ActiveRecord::SessionStore::SqlBypass
- ActiveRecord::SpawnMethods
- ActiveRecord::StaleObjectError
- ActiveRecord::StatementInvalid
- ActiveRecord::Store
- ActiveRecord::Store::ClassMethods
- ActiveRecord::TestFixtures
- ActiveRecord::TestFixtures::ClassMethods
- ActiveRecord::ThrowResult
- ActiveRecord::Timestamp
- ActiveRecord::Transactions
- ActiveRecord::Transactions::ClassMethods
- ActiveRecord::Translation
- ActiveRecord::UnknownAttributeError
- ActiveRecord::UnknownPrimaryKey
- ActiveRecord::Validations
- ActiveRecord::Validations::AssociatedValidator
- ActiveRecord::Validations::ClassMethods
- ActiveRecord::Validations::UniquenessValidator
- ActiveRecord::WrappedDatabaseException
- Mysql
- Mysql::Result
- Mysql::Stmt
- Mysql::Time
- ArJdbcMySQL
- ArJdbcMySQL::Error

No matching classes.

# ActiveRecord::QueryMethods

### Attributes

bind_values[RW]
create_with_value[RW]
eager_load_values[RW]
from_value[RW]
group_values[RW]
having_values[RW]
includes_values[RW]
joins_values[RW]
limit_value[RW]
lock_value[RW]
offset_value[RW]
order_values[RW]
preload_values[RW]
readonly_value[RW]
reordering_value[RW]
reverse_order_value[RW]
select_values[RW]
uniq_value[RW]
where_values[RW]

### Public Instance Methods

arel() click to toggle source

    # File lib/active_record/relation/query_methods.rb, line 259
    def arel
      @arel ||= with_default_scope.build_arel
    end

bind(value) click to toggle source

    # File lib/active_record/relation/query_methods.rb, line 126
    def bind(value)
      relation = clone
      relation.bind_values += [value]
      relation
    end

build_arel() click to toggle source

    # File lib/active_record/relation/query_methods.rb, line 263
    def build_arel
      arel = table.from table

      build_joins(arel, @joins_values) unless @joins_values.empty?

      collapse_wheres(arel, (@where_values - ['']).uniq)

      arel.having(*@having_values.uniq.reject{|h| h.blank?}) unless @having_values.empty?

      arel.take(connection.sanitize_limit(@limit_value)) if @limit_value
      arel.skip(@offset_value.to_i) if @offset_value

      arel.group(*@group_values.uniq.reject{|g| g.blank?}) unless @group_values.empty?

      order = @order_values
      order = reverse_sql_order(order) if @reverse_order_value
      arel.order(*order.uniq.reject{|o| o.blank?}) unless order.empty?

      build_select(arel, @select_values.uniq)

      arel.distinct(@uniq_value)
      arel.from(@from_value) if @from_value
      arel.lock(@lock_value) if @lock_value

      arel
    end

create_with(value) click to toggle source

    # File lib/active_record/relation/query_methods.rb, line 179
    def create_with(value)
      relation = clone
      relation.create_with_value = value ? create_with_value.merge(value) : {}
      relation
    end

eager_load(*args) click to toggle source

    # File lib/active_record/relation/query_methods.rb, line 25
    def eager_load(*args)
      return self if args.blank?

      relation = clone
      relation.eager_load_values += args
      relation
    end

extending(*modules) click to toggle source

Used to extend a scope with additional methods, either through a module or through a block provided.

The object returned is a relation, which can be further extended.

### Using a module

    module Pagination
      def page(number)
        # pagination code goes here
      end
    end

    scope = Model.scoped.extending(Pagination)
    scope.page(params[:page])

You can also pass a list of modules:

    scope = Model.scoped.extending(Pagination, SomethingElse)

### Using a block

    scope = Model.scoped.extending do
      def page(number)
        # pagination code goes here
      end
    end
    scope.page(params[:page])

You can also use a block and a module list:

    scope = Model.scoped.extending(Pagination) do
      def per_page(number)
        # pagination code goes here
      end
    end

    # File lib/active_record/relation/query_methods.rb, line 243
    def extending(*modules)
      modules << Module.new(&Proc.new) if block_given?

      return self if modules.empty?

      relation = clone
      relation.send(:apply_modules, modules.flatten)
      relation
    end

from(value) click to toggle source

    # File lib/active_record/relation/query_methods.rb, line 185
    def from(value)
      relation = clone
      relation.from_value = value
      relation
    end

group(*args) click to toggle source

    # File lib/active_record/relation/query_methods.rb, line 80
    def group(*args)
      return self if args.blank?

      relation = clone
      relation.group_values += args.flatten
      relation
    end

having(opts, *rest) click to toggle source

    # File lib/active_record/relation/query_methods.rb, line 140
    def having(opts, *rest)
      return self if opts.blank?

      relation = clone
      relation.having_values += build_where(opts, rest)
      relation
    end

includes(*args) click to toggle source

    # File lib/active_record/relation/query_methods.rb, line 15
    def includes(*args)
      args.reject! {|a| a.blank? }

      return self if args.empty?

      relation = clone
      relation.includes_values = (relation.includes_values + args).flatten.uniq
      relation
    end

joins(*args) click to toggle source

    # File lib/active_record/relation/query_methods.rb, line 115
    def joins(*args)
      return self if args.compact.blank?

      relation = clone

      args.flatten!
      relation.joins_values += args

      relation
    end

limit(value) click to toggle source

    # File lib/active_record/relation/query_methods.rb, line 148
    def limit(value)
      relation = clone
      relation.limit_value = value
      relation
    end

lock(locks = true) click to toggle source

    # File lib/active_record/relation/query_methods.rb, line 160
    def lock(locks = true)
      relation = clone

      case locks
      when String, TrueClass, NilClass
        relation.lock_value = locks || true
      else
        relation.lock_value = false
      end

      relation
    end

offset(value) click to toggle source

    # File lib/active_record/relation/query_methods.rb, line 154
    def offset(value)
      relation = clone
      relation.offset_value = value
      relation
    end

order(*args) click to toggle source

    # File lib/active_record/relation/query_methods.rb, line 88
    def order(*args)
      return self if args.blank?

      relation = clone
      relation.order_values += args.flatten
      relation
    end

preload(*args) click to toggle source

    # File lib/active_record/relation/query_methods.rb, line 33
    def preload(*args)
      return self if args.blank?

      relation = clone
      relation.preload_values += args
      relation
    end

readonly(value = true) click to toggle source

    # File lib/active_record/relation/query_methods.rb, line 173
    def readonly(value = true)
      relation = clone
      relation.readonly_value = value
      relation
    end

reorder(*args) click to toggle source

Replaces any existing order defined on the relation with the specified order.

    User.order('email DESC').reorder('id ASC') # generated SQL has 'ORDER BY id ASC'

Subsequent calls to order on the same relation will be appended. For example:

    User.order('email DESC').reorder('id ASC').order('name ASC')

generates a query with 'ORDER BY id ASC, name ASC'.

    # File lib/active_record/relation/query_methods.rb, line 106
    def reorder(*args)
      return self if args.blank?

      relation = clone
      relation.reordering_value = true
      relation.order_values = args.flatten
      relation
    end

reverse_order() click to toggle source

    # File lib/active_record/relation/query_methods.rb, line 253
    def reverse_order
      relation = clone
      relation.reverse_order_value = !relation.reverse_order_value
      relation
    end

select(value = Proc.new) click to toggle source

Works in two unique ways.

First: takes a block so it can be used just like Array#select.

    Model.scoped.select { |m| m.field == value }

This will build an array of objects from the database for the scope, converting them into an array and iterating through them using Array#select.

Second: Modifies the SELECT statement for the query so that only certain fields are retrieved:

    >> Model.select(:field)
    => [#<Model field:value>]

Although in the above example it looks as though this method returns an array, it actually returns a relation object and can have other query methods appended to it, such as the other methods in ActiveRecord::QueryMethods.

The argument to the method can also be an array of fields.

    >> Model.select([:field, :other_field, :and_one_more])
    => [#<Model field: "value", other_field: "value", and_one_more: "value">]

Any attributes that do not have fields retrieved by a select will raise a ActiveModel::MissingAttributeError when the getter method for that attribute is used:

    >> Model.select(:field).first.other_field
    => ActiveModel::MissingAttributeError: missing attribute: other_field

    # File lib/active_record/relation/query_methods.rb, line 70
    def select(value = Proc.new)
      if block_given?
        to_a.select {|*block_args| value.call(*block_args) }
      else
        relation = clone
        relation.select_values += Array.wrap(value)
        relation
      end
    end

uniq(value = true) click to toggle source

Specifies whether the records should be unique or not. For example:

    User.select(:name)
    # => Might return two records with the same name

    User.select(:name).uniq
    # => Returns 1 record per unique name

    User.select(:name).uniq.uniq(false)
    # => You can also remove the uniqueness

    # File lib/active_record/relation/query_methods.rb, line 201
    def uniq(value = true)
      relation = clone
      relation.uniq_value = value
      relation
    end

where(opts, *rest) click to toggle source

    # File lib/active_record/relation/query_methods.rb, line 132
    def where(opts, *rest)
      return self if opts.blank?

      relation = clone
      relation.where_values += build_where(opts, rest)
      relation
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.