edge badge
Namespace
Methods
C
D
E
F
G
H
L
N
O
R
U
V
Constants
Point = Struct.new(:x, :y)
 
UnknownAttributeError = ActiveModel::UnknownAttributeError
 

Raised when unknown attributes are supplied via mass assignment.

VALID_DIRECTIONS = [:asc, :desc, :ASC, :DESC, 'asc', 'desc', 'ASC', 'DESC']
 
Class Public methods
gem_version()

Returns the version of the currently loaded Active Record as a Gem::Version

# File activerecord/lib/active_record/gem_version.rb, line 3
def self.gem_version
  Gem::Version.new VERSION::STRING
end
version()

Returns the version of the currently loaded ActiveRecord as a Gem::Version

# File activerecord/lib/active_record/version.rb, line 5
def self.version
  gem_version
end
Instance Public methods
create_with(value)

Sets attributes to be used when creating new records from a relation object.

users = User.where(name: 'Oscar')
users.new.name # => 'Oscar'

users = users.create_with(name: 'DHH')
users.new.name # => 'DHH'

You can pass nil to create_with to reset attributes:

users = users.create_with(nil)
users.new.name # => 'Oscar'
# File activerecord/lib/active_record/relation/query_methods.rb, line 739
def create_with(value)
  spawn.create_with!(value)
end
distinct(value = true)

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).distinct
# => Returns 1 record per distinct name

User.select(:name).distinct.distinct(false)
# => You can also remove the uniqueness
Also aliased as: uniq
# File activerecord/lib/active_record/relation/query_methods.rb, line 786
def distinct(value = true)
  spawn.distinct!(value)
end
extending(*modules, &block)

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.all.extending(Pagination)
scope.page(params[:page])

You can also pass a list of modules:

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

Using a block

scope = Model.all.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.all.extending(Pagination) do
  def per_page(number)
    # pagination code goes here
  end
end
# File activerecord/lib/active_record/relation/query_methods.rb, line 836
def extending(*modules, &block)
  if modules.any? || block
    spawn.extending!(*modules, &block)
  else
    self
  end
end
from(value, subquery_name = nil)

Specifies table from which the records will be fetched. For example:

Topic.select('title').from('posts')
# => SELECT title FROM posts

Can accept other relation objects. For example:

Topic.select('title').from(Topic.approved)
# => SELECT title FROM (SELECT * FROM topics WHERE approved = 't') subquery

Topic.select('a.title').from(Topic.approved, :a)
# => SELECT a.title FROM (SELECT * FROM topics WHERE approved = 't') a
# File activerecord/lib/active_record/relation/query_methods.rb, line 767
def from(value, subquery_name = nil)
  spawn.from!(value, subquery_name)
end
having(opts, *rest)

Allows to specify a HAVING clause. Note that you can't use HAVING without also specifying a GROUP clause.

Order.having('SUM(price) > 30').group('user_id')
# File activerecord/lib/active_record/relation/query_methods.rb, line 617
def having(opts, *rest)
  opts.blank? ? self : spawn.having!(opts, *rest)
end
limit(value)

Specifies a limit for the number of records to retrieve.

User.limit(10) # generated SQL has 'LIMIT 10'

User.limit(10).limit(20) # generated SQL has 'LIMIT 20'
# File activerecord/lib/active_record/relation/query_methods.rb, line 633
def limit(value)
  spawn.limit!(value)
end
lock(locks = true)

Specifies locking settings (default to true). For more information on locking, please see ActiveRecord::Locking.

# File activerecord/lib/active_record/relation/query_methods.rb, line 660
def lock(locks = true)
  spawn.lock!(locks)
end
none()

Returns a chainable relation with zero records.

The returned relation implements the Null Object pattern. It is an object with defined null behavior and always returns an empty array of records without querying the database.

Any subsequent condition chained to the returned relation will continue generating an empty relation and will not fire any query to the database.

Used in cases where a method or scope could return zero records but the result needs to be chainable.

For example:

@posts = current_user.visible_posts.where(name: params[:name])
# => the visible_posts method is expected to return a chainable Relation

def visible_posts
  case role
  when 'Country Manager'
    Post.where(country: country)
  when 'Reviewer'
    Post.published
  when 'Bad User'
    Post.none # It can't be chained if [] is returned.
  end
end
# File activerecord/lib/active_record/relation/query_methods.rb, line 703
def none
  where("1=0").extending!(NullRelation)
end
offset(value)

Specifies the number of rows to skip before returning rows.

User.offset(10) # generated SQL has "OFFSET 10"

Should be used with order.

User.offset(10).order("name ASC")
# File activerecord/lib/active_record/relation/query_methods.rb, line 649
def offset(value)
  spawn.offset!(value)
end
readonly(value = true)

Sets readonly attributes for the returned relation. If value is true (default), attempting to update a record will result in an error.

users = User.readonly
users.first.save
=> ActiveRecord::ReadOnlyRecord: ActiveRecord::ReadOnlyRecord
# File activerecord/lib/active_record/relation/query_methods.rb, line 717
def readonly(value = true)
  spawn.readonly!(value)
end
reverse_order()

Reverse the existing order clause on the relation.

User.order('name ASC').reverse_order # generated SQL has 'ORDER BY name DESC'
# File activerecord/lib/active_record/relation/query_methods.rb, line 857
def reverse_order
  spawn.reverse_order!
end
uniq(value = true)
Alias for: distinct