aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations/collection_association.rb
Commit message (Collapse)AuthorAgeFilesLines
* Merge pull request #20997 from himesh-r/issue-20995Arthur Neves2016-02-021-1/+4
|\ | | | | | | | | Changed id-writer to save join table records based on association primary key #20995.
| * Changed id-writer to save join table records based on association primary ↵Himesh2016-02-021-1/+4
| | | | | | | | | | | | key #20995 Changed id-writer to save join table records based on association primary key
* | Bugfix collection association #create method …Bogdan Gusiev2015-11-231-4/+8
| | | | | | | | | | When same association is loaded in the model creation callback The new object is inserted into association twice
* | Delete needless `require 'active_support/deprecation'`yui-knk2015-10-201-2/+0
| | | | | | | | | | When `require 'active_support/rails'`, 'active_support/deprecation' is automatically loaded.
* | Skip statement cache on through association readerRafael Mendonça França2015-08-121-6/+1
| | | | | | | | | | | | | | If the through class has default scopes we should skip the statement cache. Closes #20745.
* | Use memoization for collection associations ids readerMehmet Emin İNAÇ2015-08-031-2/+4
|/ | | | | | Fixes #21082 remove extra space
* Deprecate force association reload by passing truePrem Sichanugrist2015-07-151-0/+8
| | | | | | | | | | | | | | | | | | This is to simplify the association API, as you can call `reload` on the association proxy or the parent object to get the same result. For collection association, you can call `#reload` on association proxy to force a reload: @user.posts.reload # Instead of @user.posts(true) For singular association, you can call `#reload` on the parent object to clear its association cache then call the association method: @user.reload.profile # Instead of @user.profile(true) Passing a truthy argument to force association to reload will be removed in Rails 5.1.
* Fix a regression introduced by removing unnecessary db call when replacingShintaro Kojima2015-04-041-0/+2
| | | | When replacing a has_many association with the same one, there is nothing to do with database but a setter method should still return the substituted value for backward compatibility.
* Reuse the CollectionAssociation#reader proxy cache if the foreign key is ↵Ben Woosley2015-03-151-2/+2
| | | | | | | | | | present from the start. When a new record has the necessary information prior to save, we can avoid busting the cache. We could simply clear the @proxy on #reset or #reset_scope, but that would clear the cache more often than necessary.
* Isolate access to .default_scopes in ActiveRecord::Scoping::DefaultBen Woosley2015-03-121-2/+1
| | | | | | | | | | | Instead use .scope_attributes? consistently in ActiveRecord to check whether there are attributes currently associated with the scope. Move the implementation of .scope_attributes? and .scope_attributes to ActiveRecord::Scoping because they don't particularly have to do specifically with Named scopes and their only dependency, in the case of .scope_attributes?, and only caller, in the case of .scope_attributes is contained in Scoping.
* Preserve Array#take(n) behaviour of HasManyAssociationAkira Matsuda2015-02-281-3/+3
|
* Merge branch 'rm-take' into 4-1-stableRafael Mendonça França2015-02-201-0/+10
|
* `type_cast_from_user` -> `cast`Sean Griffin2015-02-171-1/+1
|
* Optimize none? and one? relation query methods to use LIMIT and COUNT.Eugene Gilburg2015-02-121-2/+4
| | | | | | | | Use SQL COUNT and LIMIT 1 queries for none? and one? methods if no block or limit is given, instead of loading the entire collection to memory. The any? and many? methods already follow this behavior. [Eugene Gilburg & Rafael Mendonça França]
* Move flattening records added to an association farther outSean Griffin2015-01-261-1/+2
| | | | | | | | | There are many ways that things end up getting passed to `concat`. Not all of those entry points called `flatten` on their input. It seems that just about every method that is meant to take a single record, or that splats its input, is meant to also take an array. `concat` is the earliest point that is common to all of the methods which add records to the association. Partially fixes #18689
* Remove support to activerecord-deprecated_findersRafael Mendonça França2015-01-021-5/+1
|
* - Changed `target` to `target_reflection` to avoid warning `possible ↵Vipul A M2014-12-251-2/+2
| | | | reference to past scope`
* Pass symbol as an argument instead of a blockErik Michaels-Ober2014-11-291-5/+5
|
* Setting an association replaces records with the same id in memorySean Griffin2014-11-251-2/+18
| | | | | | | | | | | The records weren't being replaced since equality in Active Record is defined in terms of `id` only. It is reasonable to expect that the references would be replaced in memory, even if no queries are actually executed. This change did not appear to affect any other parts of the code base. I chose not to execute callbacks since we're not actually modifying the association in a way that will be persisted. Fixes #17730
* Wrap code snippets in +, not backticks, in sdocclaudiob2014-11-201-2/+2
| | | | | | | | I grepped the source code for code snippets wrapped in backticks in the comments and replaced the backticks with plus signs so they are correctly displayed in the Rails documentation. [ci skip]
* Merge pull request #11694 from ↵Rafael Mendonça França2014-11-101-1/+7
|\ | | | | | | | | | | | | | | | | Empact/association-bind-values-not-updated-on-save Fix that a collection proxy could be cached before the save of the owner, resulting in an invalid proxy lacking the owner’s id Conflicts: activerecord/CHANGELOG.md
| * Fix that a collection proxy could be cached before the save of the owner, ↵Ben Woosley2014-10-031-1/+7
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | resulting in an invalid proxy lacking the owner’s id. Absent this fix calls like: owner.association.update_all to behave unexpectedly because they try to act on association objects where owner_id is null. more evidence here: https://gist.github.com/Empact/5865555 ``` Active Record 3.2.13 -- create_table(:firms, {:force=>true}) -> 0.1371s -- create_table(:clients, {:force=>true}) -> 0.0005s 1 clients. 1 expected. 1 clients updated. 1 expected. ``` ``` Active Record 4.0.0 -- create_table(:firms, {:force=>true}) -> 0.1606s -- create_table(:clients, {:force=>true}) -> 0.0004s 1 clients. 1 expected. 0 clients updated. 1 expected. ```
* | default scopes should break the cache on has_many.Aaron Patterson2014-11-071-1/+2
| | | | | | | | | | | | if you specify a default scope on a model, it will break caching. We cannot predict what will happen inside the scope, so play it safe for now. fixes #17495
* | make sure cache is not used for collection assocations tooAaron Patterson2014-10-141-1/+6
|/ | | | follow up for #17052
* Skip StatementCache for eager loaded associations (Fixes #16761)Sammy Larbi2014-09-041-1/+1
| | | | | | Eagerly loaded collection and singular associations are ignored by the StatementCache, which causes errors when the queries they generate reference columns that were not eagerly loaded. This commit skips the creation of the StatementCache as a fix for these scenarios.
* Don't rely on the column for type casting reflectionsSean Griffin2014-06-181-2/+2
|
* Rename `type_cast` to `type_cast_from_database`Sean Griffin2014-06-091-1/+1
| | | | | | | | In some cases there is a difference between the two, we should always be doing one or the other. For convenience, `type_cast` is still a private method on type, so new types that do not need different behavior don't need to implement two methods, but it has been moved to private so it cannot be used accidentally.
* early return on delete and destroy methodseileencodes2014-05-181-0/+2
| | | | | | When delete or destroy is called on all records nothing is deleted or destroyed. Intead of running through the code and still not deleteing anything, we should early return
* rename delete_all_records to delete_or_nullify_all_recordseileencodes2014-05-131-1/+1
| | | | | | | Rename delete_all_records because this name better describes what the method is doing. We can then remove :all from the hm:t version and pull out the unoptimized call to load_target in delete_records and pass it directly.
* begin refactoring delete_records methodeileencodes2014-05-131-1/+1
| | | | | | | | | | | | | | Refactor by creating two methods delete_all_records and delete_records to be called by delete_all and delete (or destroy) respectively. This reduces the number of conditionals required to handle _how_ records get deleted. The new delete_count method handles how scope is applied to which delete action. A delete_all_records method also has to be called in has_many_through association because of how the methods are chained. This will be refactored later on.
* refactor and clean up delete_all methodeileencodes2014-05-051-9/+1
| | | | | | | | Now that delete_all with destroy or delete_all dependency behave the same we no longer need this conditional. This means we can remove the new delete_all_with_dependency method I added and go straight to delete_records from delete_all, passing :all and the dependent directly.
* flip conditional in delete_all to handle nullify bettereileencodes2014-04-281-3/+3
| | | | | | | | Nullify (or nil dependency) was doing the same thing delete_all was doing in issue #14546, creating a large IN statement if the association was loaded. Loaded and not loaded associations should behave the same. The IN statement is also not great because it's inefficient.
* simplify the delete all w/ dependency methodeileencodes2014-04-281-3/+3
| | | | | | | After reviewing this code I realized the conditional that was there previously was basically saying if the dependency is not delete all. This is a better, cleaner, and clearer way to write this method.
* remove unnecessary code from delete methodeileencodes2014-04-281-10/+2
| | | | | Now that we have a new method delete_all_with_dependency this coupled conditional is no longer needed.
* write a new method to be accessed from delete_alleileencodes2014-04-281-1/+9
| | | | | | The delete method is very coupled with delete all even though only a portion of the conditionals apply. Decoupling this will make the code easier to understand and manipulate.
* use statement cache for belongs_to relationsAaron Patterson2014-04-221-1/+1
|
* cache scope building on associationsAaron Patterson2014-04-141-1/+15
| | | | SQL statements for querying associations are now cached
* remove check for present? from delete_alleileencodes2014-04-071-2/+2
| | | | | Passing in a blank string is a bug so there's no reason to check for a blank string.
* Merge pull request #11650 from prathamesh-sonpatki/renameRafael Mendonça França2014-04-041-4/+4
|\ | | | | | | Renamed private methods _create_record and _update_record
| * [Active Record] Renamed private methods create_record and update_recordPrathamesh Sonpatki2014-02-201-4/+4
| | | | | | | | | | | | This is to ensure that they are not accidentally called by the app code. They are renamed to _create_record and _update_record respectively. Closes #11645
* | fix delete_all to remove records directlyeileencodes2014-03-311-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | When delete_all is run on a CollectionProxy and has a dependency of delete_all the SQL that is produced has an IN statement. (DELETE FROM `associated_model` where `associated_model` .`parent_id` = 1 AND `associated_model`.`id` IN (1, 2, 3...)). This only happens if the association is not loaded (both loaded and non-loaded delete_all should behave the same. This is a huge problem when it comes to deleting many records because the query becomes very slow. Instead the SQL produced should be (DELETE FROM `assoicated_model` where `associated_model`.`parent_model_id`=1). I fixed this by making sure the check for loaded and destroy also makes sure that the dependent is not delete_all, so the conditional goes to the else and deletes the records directly without the IN statement.
* | Avoid duplicated conditionalsCarlos Antonio da Silva2014-03-141-2/+1
| |
* | passing an ActiveRecord object to `exists?` is deprecated.Aaron Patterson2014-03-131-1/+1
| | | | | | | | | | Pass the id of the object to the method by calling `.id` on the AR object.
* | Remove unnecessary db call when replacing.Arthur Neves2014-02-281-1/+3
| | | | | | | | | | | | | | When replacing a has_many association with the same one, there is no need to do a round-trip to the db to create/and drop a new transaction. [fixes #14220]
* | let `insert_record` actuall save the object.Aaron Patterson2014-02-251-2/+2
| | | | | | | | | | | | | | | | | | | | `before_add` callbacks are fired before the record is saved on `has_and_belongs_to_many` assocations *and* on `has_many :through` associations. Before this change, `before_add` callbacks would be fired before the record was saved on `has_and_belongs_to_many` associations, but *not* on `has_many :through` associations. Fixes #14144
* | Revert "speed up the collection proxy reader method, but slow down the ↵Arthur Neves2014-02-201-5/+1
|/ | | | | | | | constructor" This reverts commit f9e4c3c7c0c4152b62fe9202a9d12262884bb118. [fixes #14116]
* speed up the collection proxy reader method, but slow down the constructorAaron Patterson2014-02-131-1/+5
|
* Ensure AR #second, #third, etc. finders work through associationsJason Meller2014-01-211-5/+25
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This commit fixes two regressions introduced in cafe31a078 where newly created finder methods #second, #third, #forth, and #fifth caused a NoMethodError error on reload associations and where we were pulling the wrong element out of cached associations. Examples: some_book.authors.reload.second # Before # => NoMethodError: undefined method 'first' for nil:NilClass # After # => #<Author id: 2, name: "Sally Second", ...> some_book.first.authors.first some_book.first.authors.second # Before # => #<Author id: 1, name: "Freddy First", ...> # => #<Author id: 1, name: "Freddy First", ...> # After # => #<Author id: 1, name: "Freddy First", ...> # => #<Author id: 2, name: "Sally Second", ...> Fixes #13783.
* Merge pull request #10134 from ↵Rafael Mendonça França2014-01-031-2/+2
|\ | | | | | | | | derikson/collection_proxy_select_with_multiple_args Change CollectionProxy#select to take the same arguments as ActiveRecord::select
| * Changed ActiveRecord::Associations::CollectionProxy#select to take multiple ↵Dan Erikson2013-04-081-2/+2
| | | | | | | | | | | | arguments. This makes the arguments the same as ActiveRecord::QueryMethods::select.