aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations
Commit message (Collapse)AuthorAgeFilesLines
* No need `:doc:` for `:nodoc:` classes [ci skip]Ryuta Kamizono2016-12-251-1/+1
| | | | | | Follow up to 5b14129d8d4ad302b4e11df6bd5c7891b75f393c. http://edgeapi.rubyonrails.org/classes/ActiveRecord/Attribute.html
* Privatize unneededly protected methods in Active RecordAkira Matsuda2016-12-242-9/+5
|
* Describe what we are protectingAkira Matsuda2016-12-231-0/+2
|
* Fix CI failure caused by #25227 and #25280 were merged at the same timeRyuta Kamizono2016-12-101-1/+1
|
* Merge pull request #25280 from ↵Sean Griffin2016-12-101-0/+2
|\ | | | | | | | | kamipo/prevent_range_error_for_belongs_to_associations Prevent `RangeError` for `belongs_to` associations
| * Prevent `RangeError` for `belongs_to` associationsRyuta Kamizono2016-10-101-0/+2
| | | | | | | | | | | | | | | | Currently to access `belongs_to` associations raises a `RangeError` if foreign key attribute has out of range value. It should return a nil value rather than raising a `RangeError`. Fixes #20140.
* | Resolve association class correctly when assigning ids on a through associationMatthew Draper2016-12-091-1/+1
| |
* | Add test for collection *_ids= setter when association primary key setDominic Cleal2016-11-241-1/+1
| | | | | | | | | | | | | | | | Fixes casting of IDs to the data type of the association primary key, rather than then the data type of the model's primary key. (Tests use a string primary key on the association, rather than an int.) Tests issue #20995
* | Restore RecordNotFound when *_ids= can't find records by IDDominic Cleal2016-11-241-2/+6
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 9c9fb19 changed the behaviour of the _ids= setters for associations to raise an AssociationTypeMismatch when unknown IDs are given: Class: <ActiveRecord::AssociationTypeMismatch> Message: <"Developer(#43811860) expected, got NilClass(#16732720)"> This restores the original ActiveRecord::RecordNotFound exception with a much clearer error message: Class: <ActiveRecord::RecordNotFound> Message: <"Couldn't find all Developers with 'id': (1, -9999) [WHERE \"contracts\".\"company_id\" = ?] (found 1 results, but was looking for 2)"> Fixes #25719
* | Introduce `reload_<association>` reader for singular associations.Yves Senn2016-11-222-1/+17
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This patch brings back the functionality of passing true to the association proxy. The behavior was deprecated with #20888 and scheduled for removal in Rails 5.1. The deprecation mentioned that instead of `Article.category(true)` one should use `article#reload.category`. Unfortunately the alternative does not expose the same behavior as passing true to the reader did. Specifically reloading the parent record throws unsaved changes and other caches away. Passing true only affected the association. This is problematic and there is no easy workaround. I propose to bring back the old functionality by introducing this new reader method for singular associations.
* | Merge pull request #26980 from ↵Sean Griffin2016-11-172-10/+16
|\ \ | | | | | | | | | | | | kamipo/respect_new_records_for_collection_proxy_distinct Respect new records for `CollectionProxy#uniq`
| * | Respect new records for `CollectionProxy#uniq`Ryuta Kamizono2016-11-132-10/+16
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Currently if `CollectionProxy` has more than one new record, `CollectionProxy#uniq` result is incorrect. And `CollectionProxy#uniq` was aliased to `distinct` in a1bb6c8b06db. But the `uniq` method and the `SELECT DISTINCT` method are different methods. The doc in `CollectionProxy` is for the `SELECT DISTINCT` method, not for the `uniq` method. Therefore, reverting the alias in `CollectionProxy` to fix the inconsistency and to have the both methods.
* | | Merge pull request #26905 from bogdanvlviv/docsAndrew White2016-11-132-3/+4
|\ \ \ | |/ / |/| | Add missing `+` around a some literals.
| * | Add missing `+` around a some literals.bogdanvlviv2016-10-272-3/+4
| | | | | | | | | | | | | | | | | | Mainly around `nil` [ci skip]
* | | Fixes TypeError Exception when cache counter value equals nil (#26940)Daniel E. Garcia Shulman2016-11-021-1/+1
| | | | | | | | | | | | | | | | | | * Fixes TypeError when cache counter value equals nil * Test case for counter cache on unloaded has_many association
* | | Merge pull request #26451 from kamipo/remove_target_uniq_sizeSean Griffin2016-11-011-5/+1
|\ \ \ | | | | | | | | Remove unnecessary `target.uniq.size` in `CollectionAssociation#size`
| * | | 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.
* | | | Allow `autosave: true` to be used with inverse ofSean Griffin2016-11-012-1/+11
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | With the changes in #25337, double save bugs are pretty much impossible, so we can just lift this restriction with pretty much no change. There were a handful of cases where we were relying on specific quirks in tests that had to be updated. The change to has_one associations was due to a particularly interesting test where an autosaved has_one association was replaced with a new child, where the child failed to save but the test wanted to check that the parent id persisted to `nil`. I think this is almost certainly the wrong behavior, and I may change that behavior later. But ultimately the root cause was because we never remove the parent in memory when nullifying the child. This makes #23197 no longer needed, but it is what we'll do to fix some issues on 5.0 Close #23197
* | | | Deprecate the behavior of AR::Dirty inside of after_(create|update|save) ↵Sean Griffin2016-11-014-17/+19
|/ / / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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.
* | | Add more rubocop rules about whitespacesRafael Mendonça França2016-10-293-13/+13
| | |
* | | Merge PR #19759Arthur Neves2016-10-281-1/+4
|\ \ \ | | | | | | | | | | | | Fix for has_and_belongs_to_many & has_many_through associations
| * | | Fix for has_and_belongs_to_many & has_many_through associations while ↵Mehmet Emin İNAÇ2016-02-131-1/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | partial_writes is false This will fix #19663 Also with this fix, active record does not fire unnecassary update queries while partial_writes is true
* | | | Fix HABTM associations join table resolver bug on constants and symbolsMehmet Emin İNAÇ2016-10-271-1/+1
| |/ / |/| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Using Constant and symbol class_name option for associations are valid but raises exception on HABTM associations. There was a test case which tries to cover symbol class_name usage but doesn't cover correctly. Fixed both symbol usage and constant usage as well. These are all working as expected now; ``` has_and_belongs_to_many :foos, class_name: 'Foo' has_and_belongs_to_many :foos, class_name: :Foo has_and_belongs_to_many :foos, class_name: Foo ``` Closes #23767
* | | Fix regression caused due to removal of select method from CollectionAssociationPrathamesh Sonpatki2016-10-221-6/+0
| |/ |/| | | | | | | | | | | | | | | | | | | | | | | | | | | | | - CollectionAssociation#select was removed in https://github.com/rails/rails/pull/25989 in favor of QueryMethods#select but it caused a regression when passing arguments to select and a block. - This used to work earlier in Rails 4.2 and Rails 5. See gist https://gist.github.com/prathamesh-sonpatki/a7df922273473a77dfbc742a4be4b618. - This commit restores the behavior of Rails 4.2 and Rails 5.0.0 to allow passing arguments and block at the same time but also deprecates it. - Because, these arguments do not have any effect on the output of select when select is used with a block. - Updated documentation to remove the example passing arguments and block at the same time to `CollectionProxy#select`.
* | Don't skip in-memory insertion of associations when loaded in validateSean Griffin2016-09-292-7/+12
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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.
* | Merge pull request #26448 from kamipo/remove_collection_association_uniqKasper Timm Hansen2016-09-181-1/+0
|\ \ | | | | | | Remove unnecessry `alias uniq distinct` for collection association
| * | Remove unnecessry `alias uniq distinct` for collection associationRyuta Kamizono2016-09-101-1/+0
| | | | | | | | | | | | `CollectionAssociation` is internal class and `uniq` is not called.
* | | Fix broken comments indentation caused by rubocop auto-correct [ci skip]Ryuta Kamizono2016-09-141-17/+17
|/ / | | | | | | | | | | All indentation was normalized by rubocop auto-correct at 80e66cc4d90bf8c15d1a5f6e3152e90147f00772. But comments was still kept absolute position. This commit aligns comments with method definitions for consistency.
* | Merge pull request #26380 from kamipo/pass_set_inverse_instance_blockKasper Timm Hansen2016-09-081-2/+2
|\ \ | | | | | | Pass `set_inverse_instance` block to `sc.execute` for `SingularAssociation`
| * | Pass `set_inverse_instance` block to `sc.execute` for `SingularAssociation`Ryuta Kamizono2016-09-031-2/+2
| | | | | | | | | | | | | | | | | | | | | Follow up to caa178c. caa178c updated all code which sets inverse instances on newly loaded associations to use block. But `SingularAssociation` was forgotten it.
* | | Revert " [ci skip] Remove duplicate example."Vipul A M2016-09-061-0/+5
| | |
* | | Merge pull request #26410 from aditya-kapoor/rm-dupsVipul A M2016-09-061-5/+0
|\ \ \ | | | | | | | | [ci skip] Remove duplicate example.
| * | | [ci skip] Remove duplicate example.Aditya Kapoor2016-09-061-5/+0
| | | |
* | | | Remove redundant `!loaded?` conditionRyuta Kamizono2016-09-061-2/+2
| | | | | | | | | | | | | | | | | | | | Already checked `if !find_target? || loaded?`, unnecessary `!loaded?` in elsif condition.
* | | | Merge pull request #26379 from kamipo/remove_unnecessary_query_scopeAndrew White2016-09-051-4/+0
|\ \ \ \ | |/ / / |/| | | Remove unnecessary `query_scope`
| * | | Remove unnecessary `query_scope`Ryuta Kamizono2016-08-161-4/+0
| | | |
* | | | Remove unnecessary `count` method for collection proxyRyuta Kamizono2016-09-042-36/+14
| | | | | | | | | | | | | | | | Simply use its own method because `CollectionProxy` inherits `Relation`.
* | | | Extract duplicated `create` and `create!` definition for associationRyuta Kamizono2016-09-033-16/+8
| |/ / |/| |
* | | fixes remaining RuboCop issues [Vipul A M, Xavier Noria]Xavier Noria2016-09-012-3/+3
| | |
* | | Ensure that inverse associations are set before running callbacksSean Griffin2016-08-316-13/+27
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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-192-28/+12
| | | | | | | | | | | | Simply use its own methods because `CollectionProxy` inherits `Relation`.
* | | Remove unnecessary `length` method for collection proxyRyuta Kamizono2016-08-192-12/+6
| | | | | | | | | | | | | | | | | | | | | `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-192-11/+6
|\ \ \ | | | | | | | | | | | | | | | | kamipo/remove_unnecessary_select_for_collection_proxy Remove unnecessary `select` method for `CollectionProxy`
| * | | Remove unnecessary `select` method for `CollectionProxy`Ryuta Kamizono2016-08-182-11/+6
| | | | | | | | | | | | | | | | | | | | | | | | | | | | 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
* | | | Merge pull request #24099 from k0kubun/preserve-readonlyRafael Mendonça França2016-08-182-2/+6
|\ \ \ \ | | | | | | | | | | | | | | | Preserve readonly flag only for readonly association
| * | | | Preserve readonly flag only for readonly associationTakashi Kokubun2016-07-302-2/+4
| | | | | | | | | | | | | | | | | | | | Fixes #24093
* | | | | Remove unnecessary ordinal methods for collection associationRyuta Kamizono2016-08-182-91/+74
| |/ / / |/| | | | | | | | | | | | | | | Currently `CollectionProxy` inherits `Relation` therefore we can use its own methods rather than delegating to collection association.
* | | | Merge pull request #25976 from kamipo/pluck_uses_loaded_targetRafael França2016-08-171-1/+12
|\ \ \ \ | | | | | | | | | | `pluck` should use `records` (`load_target`) when `loaded?` is true