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
FinderMethods - Included
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
- #==(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. |
#create | Tries to create a new record with the same scoped attributes defined in the relation. |
#create! | Similar to #create, but calls |
#delete | Deletes the row with a primary key matching the |
#delete_all | Deletes the records matching |
#destroy | Destroy an object (or multiple objects) that has the given id. |
#destroy_all | Destroys the records matching |
#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_initialize_by | |
#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 |
|
#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 |
#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 |
#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 |
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 |
#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. |
#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. |
#uniq | Alias for QueryMethods#distinct. |
#uniq! | Alias for QueryMethods#distinct!. |
#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 |
#find_in_batches | Yields each batch of records that was found by the find |
Constructor Details
.new(klass, table, association) ⇒ AssociationRelation
# 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.
# 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