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
QueryMethods - Included
DEFAULT_VALUES, FROZEN_EMPTY_ARRAY, FROZEN_EMPTY_HASH, STRUCTURAL_OR_METHODS, VALID_UNSCOPING_VALUES
FinderMethods - Included
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 |
Instance Method Summary
- #==(other)
- #build(*args, &block) (also: #new)
- #create(*args, &block)
- #create!(*args, &block)
-
#new(*args, &block)
Alias for #build.
- #proxy_association
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! | |
| #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 |
| #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_initialize_by | Like |
| #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 |
| #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 |
| #fifth | Find the fifth record. |
| #fifth! | Same as |
| #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 |
| #first | Find the first record (or first N records if a parameter is supplied). |
| #first! | Same as |
| #forty_two | Find the forty-second record. |
| #forty_two! | Same as |
| #fourth | Find the fourth record. |
| #fourth! | Same as |
| #last | Find the last record (or last N records if a parameter is supplied). |
| #last! | Same as |
| #second | Find the second record. |
| #second! | Same as |
| #second_to_last | Find the second-to-last record. |
| #second_to_last! | Same as |
| #take | Gives a record (or N records if a parameter is supplied) without any implied order. |
| #take! | Same as |
| #third | Find the third record. |
| #third! | Same as |
| #third_to_last | Find the third-to-last record. |
| #third_to_last! | Same as |
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 |
| #sum | Calculates the sum of values on a given column. |
SpawnMethods - Included
| #except | Removes from the query the condition(s) specified in |
| #merge | Merges in the conditions from |
| #only | Removes any condition from the query other than the one(s) specified in |
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 |
| #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 |
| #left_joins | Alias for QueryMethods#left_outer_joins. |
| #left_outer_joins | Performs a left outer joins on |
| #limit | Specifies a limit for the number of records to retrieve. |
| #lock | Specifies locking settings (default to |
| #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 |
| #readonly | Sets readonly attributes for the returned relation. |
| #references | Use to indicate that the given |
| #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 |
::Enumerable - Included
Constructor Details
.new(klass, association) ⇒ AssociationRelation
# 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.
# 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