123456789_123456789_123456789_123456789_123456789_

Class: ActiveRecord::AssociationRelation

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Relation
Instance Chain:
Inherits: ActiveRecord::Relation
Defined in: activerecord/lib/active_record/association_relation.rb

Constant Summary

QueryMethods - Included

VALID_UNSCOPING_VALUES

FinderMethods - Included

ONE_AS_ONE

Relation - Inherited

INVALID_METHODS_FOR_DELETE_ALL, MULTI_VALUE_METHODS, SINGLE_VALUE_METHODS, VALUE_METHODS

Class Method Summary

Instance Attribute Summary

Relation - Inherited

#any?

Returns true if there are any records.

#blank?

Returns true if relation is blank.

#eager_loading?

Returns true if relation needs eager loading.

#empty?

Returns true if there are no records.

#klass,
#loaded?

Alias for Relation#loaded.

#many?

Returns true if there is more than one record.

#model

Alias for Relation#klass.

#table

Instance Method Summary

Relation - Inherited

#==

Compares two relations for equality.

#build

Alias for Relation#new.

#create

Tries to create a new record with the same scoped attributes defined in the relation.

#create!

Similar to #create, but calls create! on the base class.

#delete

Deletes the row with a primary key matching the id argument, using a SQL DELETE statement, and returns the number of rows deleted.

#delete_all

Deletes the records matching conditions without instantiating the records first, and hence not calling the destroy method nor invoking callbacks.

#destroy

Destroy an object (or multiple objects) that has the given id.

#destroy_all

Destroys the records matching conditions by instantiating each record and calling its destroy method.

#encode_with

Serializes the relation objects ::Array.

#explain

Runs EXPLAIN on the query or queries triggered by this relation and returns the result as a string.

#find_or_create_by

Finds the first record with the given attributes, or creates a record with the attributes if one is not found:

#find_or_create_by!

Like find_or_create_by, but calls #create! so an exception is raised if the created record is invalid.

#find_or_initialize_by

Like find_or_create_by, but calls .new instead of #create.

#initialize_copy, #inspect,
#joined_includes_values

Joins that are also marked for preloading.

#load

Causes the records to be loaded from the database if they have not been loaded already.

#loaded,
#new

Initializes new record from relation while maintaining the current scope.

#pretty_print,
#reload

Forces reloading of relation.

#reset, #scope_for_create,
#scoping

Scope all queries to the current scope.

#size

Returns size of the records.

#to_a

Converts relation objects to ::Array.

#to_sql

Returns sql statement for the relation.

#uniq_value

uniq and uniq! are silently deprecated.

#update

Updates an object (or multiple objects) and saves it to the database, if validations pass.

#update_all

Updates all records in the current relation with details given.

#values,
#where_values_hash

Returns a hash of where conditions.

FinderMethods - Included

#exists?

Returns true if a record exists in the table that matches the id or conditions given, or false otherwise.

#fifth

Find the fifth record.

#fifth!

Same as fifth but raises RecordNotFound if no record is found.

#find

Find by id - This can either be a specific id (1), a list of ids (1, 5, 6), or an array of ids ([5, 6, 10]).

#find_by

Finds the first record matching the specified conditions.

#find_by!

Like find_by, except that if no record is found, raises an RecordNotFound error.

#first

Find the first record (or first N records if a parameter is supplied).

#first!

Same as first but raises RecordNotFound if no record is found.

#forty_two

Find the forty-second record.

#forty_two!

Same as forty_two but raises RecordNotFound if no record is found.

#fourth

Find the fourth record.

#fourth!

Same as fourth but raises RecordNotFound if no record is found.

#last

Find the last record (or last N records if a parameter is supplied).

#last!

Same as last but raises RecordNotFound if no record is found.

#second

Find the second record.

#second!

Same as second but raises RecordNotFound if no record is found.

#take

Gives a record (or N records if a parameter is supplied) without any implied order.

#take!

Same as take but raises RecordNotFound if no record is found.

#third

Find the third record.

#third!

Same as third but raises RecordNotFound if no record is found.

Calculations - Included

#average

Calculates the average value on a given column.

#calculate

This calculates aggregate values in the given column.

#count

Count the records.

#ids

Pluck all the ID's for the relation using the table's primary key.

#maximum

Calculates the maximum value on a given column.

#minimum

Calculates the minimum value on a given column.

#pluck

Use pluck as a shortcut to select one or more attributes without loading a bunch of records just to grab the attributes you want.

#sum

Calculates the sum of values on a given column.

SpawnMethods - Included

#except

Removes from the query the condition(s) specified in skips.

#merge

Merges in the conditions from other, if other is an Relation.

#only

Removes any condition from the query other than the one(s) specified in onlies.

QueryMethods - Included

#create_with

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

#distinct

Specifies whether the records should be unique or not.

#eager_load

Forces eager loading by performing a LEFT OUTER JOIN on args:

#extending

Used to extend a scope with additional methods, either through a module or through a block provided.

#extensions,
#from

Specifies table from which the records will be fetched.

#group

Allows to specify a group attribute:

#having

Allows to specify a HAVING clause.

#includes

Specify relationships to be included in the result set.

#joins

Performs a joins on args:

#limit

Specifies a limit for the number of records to retrieve.

#lock

Specifies locking settings (default to true).

#none

Returns a chainable relation with zero records.

#offset

Specifies the number of rows to skip before returning rows.

#order

Allows to specify an order attribute:

#preload

Allows preloading of args, in the same way that includes does:

#readonly

Sets readonly attributes for the returned relation.

#references

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.

#reorder

Replaces any existing order defined on the relation with the specified order.

#reverse_order

Reverse the existing order clause on the relation.

#rewhere

Allows you to change a previously set where condition for a given attribute, instead of appending to that condition.

#select

Works in two unique ways.

#uniq
#uniq!
#unscope

Removes an unwanted relation that is already defined on a chain of relations.

#where

Returns a new relation, which is the result of filtering the current relation according to the conditions in the arguments.

Batches - Included

#find_each

Looping through a collection of records from the database (using the all method, for example) is very inefficient since it will try to instantiate all the objects at once.

#find_in_batches

Yields each batch of records that was found by the find options as an array.

Constructor Details

.new(klass, table, association) ⇒ AssociationRelation

[ GitHub ]

  
# File 'activerecord/lib/active_record/association_relation.rb', line 3

def initialize(klass, table, association)
  super(klass, table)
  @association = association
end

Instance Method Details

#==(other)

[ GitHub ]

  
# File 'activerecord/lib/active_record/association_relation.rb', line 12

def ==(other)
  other == to_a
end

#build(*args, &block) Also known as: #new

[ GitHub ]

  
# File 'activerecord/lib/active_record/association_relation.rb', line 16

def build(*args, &block)
  scoping { @association.build(*args, &block) }
end

#create(*args, &block)

[ GitHub ]

  
# File 'activerecord/lib/active_record/association_relation.rb', line 21

def create(*args, &block)
  scoping { @association.create(*args, &block) }
end

#create!(*args, &block)

[ GitHub ]

  
# File 'activerecord/lib/active_record/association_relation.rb', line 25

def create!(*args, &block)
  scoping { @association.create!(*args, &block) }
end

#new(*args, &block)

Alias for #build.

[ GitHub ]

  
# File 'activerecord/lib/active_record/association_relation.rb', line 19

alias new build

#proxy_association

[ GitHub ]

  
# File 'activerecord/lib/active_record/association_relation.rb', line 8

def proxy_association
  @association
end