lock

lock(locks = true) Instance Public methods Specifies locking settings (default to true). For more information on locking, please see ActiveRecord::Locking.

limit

limit(value) Instance Public methods 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'

joins

joins(*args) Instance Public methods Performs a joins on args: User.joins(:posts) => SELECT "users".* FROM "users" INNER JOIN "posts" ON "posts"."user_id" = "users"."id" You can use strings in order to customize your joins: User.joins("LEFT JOIN bookmarks ON bookmarks.bookmarkable_type = 'Post' AND bookmarks.user_id = users.id") => SELECT "users".* FROM "users" LEFT JOIN bookmarks ON bookmarks.bookmarkable_type = 'Post' AND bookmarks.user_id = users.id

includes

includes(*args) Instance Public methods Specify relationships to be included in the result set. For example: users = User.includes(:address) users.each do |user| user.address.city end allows you to access the address attribute of the User model without firing an additional query. This will often result in a performance improvement over a simple join. You can also specify multiple relationships, like this: users = User.includes(:address, :friends) Loading nested relationships is

having

having(opts, *rest) Instance Public methods 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')

group

group(*args) Instance Public methods Allows to specify a group attribute: User.group(:name) => SELECT "users".* FROM "users" GROUP BY name Returns an array with distinct records based on the group attribute: User.select([:id, :name]) => [#<User id: 1, name: "Oscar">, #<User id: 2, name: "Oscar">, #<User id: 3, name: "Foo"> User.group(:name) => [#<User id: 3, name: "Foo", ...>, #<User id: 2, name: "Oscar", ...>] User.group('name AS grouped_na

from

from(value, subquery_name = nil) Instance Public methods 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')

extending

extending(*modules, &block) Instance Public 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.all.extending(Pagination) scope.page(params[:page]) You can also pass a list of modules: scope = Model.all.extending(Pagination, SomethingElse) Using a

eager_load

eager_load(*args) Instance Public methods Forces eager loading by performing a LEFT OUTER JOIN on args: User.eager_load(:posts) => SELECT "users"."id" AS t0_r0, "users"."name" AS t0_r1, ... FROM "users" LEFT OUTER JOIN "posts" ON "posts"."user_id" = "users"."id"

distinct

distinct(value = true) Instance Public methods 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 uniq