Module ActiveRecord::QueryMethods
In: 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  

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

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

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’.

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

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

[Validate]