aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations/collection_association.rb
Commit message (Collapse)AuthorAgeFilesLines
...
| * | Remove unnecessary `target.uniq.size` in `CollectionAssociation#size`Ryuta Kamizono2016-10-291-5/+1
| |/ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | If `association_scope` have `distinct_value`, same record cannot exist in `target`. https://github.com/rails/rails/blob/v5.0.0/activerecord/lib/active_record/associations/collection_association.rb#L419-L424 ```ruby def add_to_target(record, skip_callbacks = false, &block) if association_scope.distinct_value index = @target.index(record) end replace_on_target(record, index, skip_callbacks, &block) end ```
* | Merge pull request #26453 from kamipo/remove_unused_internal_dependent_optionSean Griffin2016-11-011-4/+1
|\ \ | | | | | | Remove unused internal `:dependent` option in `CollectionAssociation#delete`
| * | Remove unused internal `:dependent` option in `CollectionAssociation#delete`Ryuta Kamizono2016-10-291-4/+1
| |/ | | | | | | | | The internal `:dependent` option was introduced at #10604. But currently unused.
* / Deprecate the behavior of AR::Dirty inside of after_(create|update|save) ↵Sean Griffin2016-11-011-1/+1
|/ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | callbacks We pretty frequently get bug reports that "dirty is broken inside of after callbacks". Intuitively they are correct. You'd expect `Model.after_save { puts changed? }; model.save` to do the same thing as `model.save; puts model.changed?`, but it does not. However, changing this goes much farther than just making the behavior more intuitive. There are a _ton_ of places inside of AR that can be drastically simplified with this change. Specifically, autosave associations, timestamps, touch, counter cache, and just about anything else in AR that works with callbacks have code to try to avoid "double save" bugs which we will be able to flat out remove with this change. We introduce two new sets of methods, both with names that are meant to be more explicit than dirty. The first set maintains the old behavior, and their names are meant to center that they are about changes that occurred during the save that just happened. They are equivalent to `previous_changes` when called outside of after callbacks, or once the deprecation cycle moves. The second set is the new behavior. Their names imply that they are talking about changes from the database representation. The fact that this is what we really care about became clear when looking at `BelongsTo.touch_record` when tests were failing. I'm unsure that this set of methods should be in the public API. Outside of after callbacks, they are equivalent to the existing methods on dirty. Dirty itself is not deprecated, nor are the methods inside of it. They will only emit the warning when called inside of after callbacks. The scope of this breakage is pretty large, but the migration path is simple. Given how much this can improve our codebase, and considering that it makes our API more intuitive, I think it's worth doing.
* Don't skip in-memory insertion of associations when loaded in validateSean Griffin2016-09-291-7/+8
| | | | | | | | | | | | | | | | This was caused by 6d0d83a33f59d9415685852cf77818c41e2e2700. While the bug it's trying to fix is handled if the association is loaded in an after_(create|save) callback, it doesn't handle any cases that load the association before the persistence takes place (validation, or before_* filters). Instead of caring about the timing of persistence, we can just ensure that we're not double adding the record instead. The test from that commit actually broke, but it was not because the bug has been re-introduced. It was because `Bulb` in our test suite is doing funky things that look like STI but isn't STI, so equality comparison didn't happen as the loaded model was of a different class. Fixes #26661.
* Remove unnecessry `alias uniq distinct` for collection associationRyuta Kamizono2016-09-101-1/+0
| | | | `CollectionAssociation` is internal class and `uniq` is not called.
* Remove redundant `!loaded?` conditionRyuta Kamizono2016-09-061-2/+2
| | | | | Already checked `if !find_target? || loaded?`, unnecessary `!loaded?` in elsif condition.
* Remove unnecessary `count` method for collection proxyRyuta Kamizono2016-09-041-25/+0
| | | | Simply use its own method because `CollectionProxy` inherits `Relation`.
* Extract duplicated `create` and `create!` definition for associationRyuta Kamizono2016-09-031-8/+0
|
* Ensure that inverse associations are set before running callbacksSean Griffin2016-08-311-3/+3
| | | | | | | | | | | | | | | | | If a parent association was accessed in an `after_find` or `after_initialize` callback, it would always end up loading the association, and then immediately overwriting the association we just loaded. If this occurred in a way that the parent's `current_scope` was set to eager load the child, this would result in an infinite loop and eventually overflow the stack. For records that are created with `.new`, we have a mechanism to perform an action before the callbacks are run. I've introduced the same code path for records created with `instantiate`, and updated all code which sets inverse instances on newly loaded associations to use this block instead. Fixes #26320.
* Remove unnecessary `any?` and `many?` methods for collection proxyRyuta Kamizono2016-08-191-22/+0
| | | | Simply use its own methods because `CollectionProxy` inherits `Relation`.
* Remove unnecessary `length` method for collection proxyRyuta Kamizono2016-08-191-9/+0
| | | | | | | `length` is delegated to `records` (`load_target`) by `ActiveRecord::Delegation`. https://github.com/rails/rails/blob/v5.0.0/activerecord/lib/active_record/relation/delegation.rb#L38
* Merge pull request #25989 from ↵Rafael França2016-08-191-8/+0
|\ | | | | | | | | kamipo/remove_unnecessary_select_for_collection_proxy Remove unnecessary `select` method for `CollectionProxy`
| * Remove unnecessary `select` method for `CollectionProxy`Ryuta Kamizono2016-08-181-8/+0
| | | | | | | | | | | | | | Currently `CollectionProxy` inherits `Relation` and `Relation` includes `QueryMethods`. This method is completely duplicated. https://github.com/rails/rails/blob/v5.0.0/activerecord/lib/active_record/relation/query_methods.rb#L271-L275
* | Remove unnecessary ordinal methods for collection associationRyuta Kamizono2016-08-181-65/+6
|/ | | | | Currently `CollectionProxy` inherits `Relation` therefore we can use its own methods rather than delegating to collection association.
* Fix inconsistent the signature of finder methods for collection associationRyuta Kamizono2016-08-161-22/+20
| | | | | | | `#second`, `#third`, etc finder methods was added in 03855e790de2224519f55382e3c32118be31eeff. But the signature of these methods is inconsistent with the original finder methods. And also the signature of `#first` and `#last` methods is different from the original. This commit fixes the inconsistency.
* `CollectionProxy#take` should respect dirty targetRyuta Kamizono2016-08-141-13/+9
| | | | | `#first`, `#second`, ..., `#last` methods respects dirty target. But `#take` doesn't respect it. This commit fixes the inconsistent behavior.
* revises most Lint/EndAlignment offensesXavier Noria2016-08-071-5/+5
| | | | Some case expressions remain, need to think about those ones.
* applies remaining conventions across the projectXavier Noria2016-08-061-1/+0
|
* normalizes indentation and whitespace across the projectXavier Noria2016-08-061-1/+1
|
* Avoid duplicated `set_inverse_instance` for target scopeRyuta Kamizono2016-08-031-15/+12
| | | | | | | | | Because `scope` (`target_scope`) is a `AssociationRelation`. `AssociationRelation` handles `set_inverse_instance`. https://github.com/rails/rails/blob/v5.0.0/activerecord/lib/active_record/association_relation.rb#L31-L33 See also #26022.
* Remove unnecessary `set_inverse_instance` in finder methodsRyuta Kamizono2016-08-021-6/+2
| | | | | | | Because `scope` (`target_scope`) is a `AssociationRelation`. `AssociationRelation` handles `set_inverse_instance`. https://github.com/rails/rails/blob/v5.0.0/activerecord/lib/active_record/association_relation.rb#L31-L33
* Merge pull request #24862 from maclover7/jm-ar-fixesRafael França2016-05-211-2/+2
|\ | | | | Add missing `the`
| * Add missing `the`Jon Moss2016-05-211-2/+2
| | | | | | | | [ci skip]
* | Support for unified Integer class in Ruby 2.4+Jeremy Daer2016-05-181-2/+2
| | | | | | | | | | | | | | | | Ruby 2.4 unifies Fixnum and Bignum into Integer: https://bugs.ruby-lang.org/issues/12005 * Forward compat with new unified Integer class in Ruby 2.4+. * Backward compat with separate Fixnum/Bignum in Ruby 2.2 & 2.3. * Drops needless Fixnum distinction in docs, preferring Integer.
* | Forward ActiveRecord::Relation#count to Enumerable#count if block givenErik Michaels-Ober2016-03-191-2/+5
|/
* rename to 'second_to_last' and 'third_to_last'Brian Christian2016-02-101-4/+4
|
* allow Array.penultimate and Array.antepenultiate access methodsBrian Christian2016-02-091-0/+8
|
* 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. ```