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

Batches - Included

ORDER_IGNORE_MESSAGE

QueryMethods - Included

DEFAULT_VALUES, FROZEN_EMPTY_ARRAY, FROZEN_EMPTY_HASH, STRUCTURAL_OR_METHODS, VALID_UNSCOPING_VALUES

FinderMethods - Included

ONE_AS_ONE

Relation - Inherited

CLAUSE_METHODS, 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.

#none?

Returns true if there are no records.

#one?

Returns true if there is exactly one record.

#predicate_builder, #table

::Enumerable - Included

#many?

Returns true if the enumerable has more than 1 element.

Instance Method Summary

Relation - Inherited

#==

Compares two relations for equality.

#build

Alias for Relation#new.

#cache_key

Returns a cache key that can be used to identify the records fetched by this query.

#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_all

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

#destroy_all

Destroys the records 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, #locked?,
#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

Alias for Relation#to_ary.

#to_ary

Converts relation objects to ::Array.

#to_sql

Returns sql statement for the relation.

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

#second_to_last

Find the second-to-last record.

#second_to_last!

Same as #second_to_last 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.

#third_to_last

Find the third-to-last record.

#third_to_last!

Same as #third_to_last 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.

#left_joins
#left_outer_joins

Performs a left outer 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.

#or

Returns a new relation, which is the logical union of this relation and the one passed as an argument.

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

#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 Scoping::Named::ClassMethods#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.

#in_batches

Yields Relation objects to work with a batch of records.

::Enumerable - Included

#exclude?

The negative of the Enumerable#include?.

#index_by

Convert an enumerable to a hash.

#pluck

Convert an enumerable to an array based on the given key.

#sum

Calculates a sum from the elements.

#without

Returns a copy of the enumerable without the specified elements.

Constructor Details

.new(klass, association) ⇒ AssociationRelation

[ GitHub ]

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

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

Instance Method Details

#==(other)

[ GitHub ]

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

def ==(other)
  other == records
end

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

[ GitHub ]

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

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

#create(*args, &block)

[ GitHub ]

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

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

#create!(*args, &block)

[ GitHub ]

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

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 21

alias new build

#proxy_association

[ GitHub ]

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

def proxy_association
  @association
end