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'

lock

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

none

none() Instance Public methods 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

offset

offset(value) Instance Public methods 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")

order

order(*args) Instance Public methods Allows to specify an order attribute: User.order('name') => SELECT "users".* FROM "users" ORDER BY name User.order('name DESC') => SELECT "users".* FROM "users" ORDER BY name DESC User.order('name DESC, email') => SELECT "users".* FROM "users" ORDER BY name DESC, email User.order(:name) => SELECT "users".* FROM "users" ORDER BY "users"."name" ASC User.order(email: :desc) => SELECT "users".* FROM "users" ORDER BY "users"."email

preload

preload(*args) Instance Public methods Allows preloading of args, in the same way that includes does: User.preload(:posts) => SELECT "posts".* FROM "posts" WHERE "posts"."user_id" IN (1, 2, 3)

readonly

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

references

references(*table_names) Instance Public methods Use to indicate that the given table_names are referenced by an SQL string, and should therefore be JOINed in any query rather than loaded separately. This method only works in conjuction with includes. See includes for more details. User.includes(:posts).where("posts.name = 'foo'") # => Doesn't JOIN the posts table, resulting in an error. User.includes(:posts).where("posts.name = 'foo'").references(:posts) # => Query now know

reorder

reorder(*args) Instance Public methods 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'.

reverse_order

reverse_order() Instance Public methods Reverse the existing order clause on the relation. User.order('name ASC').reverse_order # generated SQL has 'ORDER BY name DESC'