select

select(*fields) Instance Public methods Works in two unique ways. First: takes a block so it can be used just like Array#select. Model.all.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 th

rewhere

rewhere(conditions) Instance Public methods Allows you to change a previously set where condition for a given attribute, instead of appending to that condition. Post.where(trashed: true).where(trashed: false) # => WHERE `trashed` = 1 AND `trashed` = 0 Post.where(trashed: true).rewhere(trashed: false) # => WHERE `trashed` = 0 Post.where(active: true).where(trashed: true).rewhere(trashed: false) # => WHERE `active` = 1 AND `trashed`

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'

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

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

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

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)

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

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")

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