aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations
Commit message (Collapse)AuthorAgeFilesLines
* Fix select with block doesn't return newly built records in has_many associationRyuta Kamizono2017-03-091-2/+2
| | | | | | | | The `select` in `QueryMethods` is also an enumerable method. Enumerable methods with block should delegate to `records` on `CollectionProxy`, not `scope`. Fixes #28348.
* remove unused parametersAaron Patterson2017-03-031-6/+6
|
* `join_keys` no longer needs a class passed to itAaron Patterson2017-03-032-3/+3
| | | | | | Reflections only use their own information to create a `join_keys` object. This means that we can call `join_keys` on a reflection object and have it be context-free.
* ask reflection for klass join reflectionAaron Patterson2017-03-031-13/+1
|
* Move join scopes on to the reflection objectAaron Patterson2017-03-031-8/+1
| | | | | | | Scopes can only ever be *not* reflection objects when they are passed in to the Reflection constructor. Given this fact, we can eliminate is_a checks and an intermediate array object by just asking the reflection object for join scopes.
* Remove `node` parameter to `join_constraints`Aaron Patterson2017-03-032-3/+3
| | | | | I don't think we actually need this parameter anymore. Nobody seems to be using it.
* Revert "Dupping a CollectionProxy should dup the load_target"eileencodes2017-02-281-4/+0
| | | | | | | | | I incorrectly changed behavior of `dup`. Reading the original issue I thought that `dup` should retain the original contents of the record and it's associations but it is in fact supposed to be a copy as if a record had been reinitialized. This reverts commit ca8c21df0fdbf1f03ba2f7fb16b39c3282dc1be0.
* Dupping a CollectionProxy should dup the load_targeteileencodes2017-02-281-0/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | In Rails 3.2 dupping a `CollectionProxy` would dup it's `load_target` as well. That functionality has been broken since the release of Rails 4.0. I hit this in an application upgrade and wondered why duplicating a CollectionProxy and assigning it to a variable stopped working. When calling `dup` on a `CollectionProxy` only the owner (ex. topic) was getting duplicated and the `load_target` would remain in tact with it's original object ID. Dupping the `load_target` is useful for performing a logging operation after records have been destroyed in a method. For example: ``` def transfer_operation saved_replies = topic.replies topic.replies.clear saved_replies.each do |reply| user.update_replies_count! end end ``` This change adds a `initialize_dup` method that performs a `deep_dup` on the `@associatiation` so that the `load_target` is dupped as well. Fixes #17117
* Merge pull request #25877 from kamipo/delegate_to_scope_rather_than_mergeMatthew Draper2017-02-213-24/+29
|\ | | | | Delegate to `scope` rather than `merge!` for collection proxy
| * Define `respond_to_missing?` instead of `respond_to?`Ryuta Kamizono2017-02-211-4/+4
| |
| * Cache target scope for collection proxyRyuta Kamizono2016-12-252-3/+4
| |
| * No need to cache collection proxies separatelyRyuta Kamizono2016-12-251-7/+1
| | | | | | | | Because merging the association scope was removed.
| * Delegate to `scope` rather than `merge!` for collection proxyRyuta Kamizono2016-12-252-14/+24
| | | | | | | | | | `merge! association.scope(nullify: false)` is expensive but most methods do not need the merge.
* | Chain scope constraints should respect own table aliasRyuta Kamizono2017-02-011-5/+6
| | | | | | | | Fixes #27666.
* | Merge pull request #27838 from kamipo/reload_destroyed_through_recordRafael França2017-01-311-0/+4
|\ \ | | | | | | Reload `through_record` that has been destroyed in `create_through_record`
| * | Reload `through_record` that has been destroyed in `create_through_record`Ryuta Kamizono2017-01-291-0/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This is an alternative of #27714. If `has_one :through` association has set `nil`, `through_record` is destroyed but still remain loaded target in `through_proxy` until `reload` or `reset` explicitly. If `through_proxy` is not reset (remain destroyed (frozen) target), setting new record causes `RuntimeError: Can't modify frozen hash`. To prevent `RuntimeError`, should reload `through_record` that has been destroyed in `create_through_record`.
* | | Fix `scopes` implementation on `PolymorphicReflection`Aaron Patterson2017-01-302-7/+3
|/ / | | | | | | `PolymorphicReflection` needs to be custom for handling scope lambdas
* | class Foo < Struct.new(:x) creates an extra unneeded anonymous classAkira Matsuda2017-01-131-1/+1
| | | | | | | | because Struct.new returns a Class, we just can give it a name and use it directly without inheriting from it
* | Reduce string objects by using \ instead of + or << for concatenating stringsAkira Matsuda2017-01-121-1/+1
| | | | | | | | (I personally prefer writing one string in one line no matter how long it is, though)
* | `self.` is not needed when calling its own instance methodAkira Matsuda2017-01-051-1/+1
| | | | | | | | Actually, private methods cannot be called with `self.`, so it's not just redundant, it's a bad habit in Ruby
* | Merge pull request #26352 from kamipo/avoid_to_call_set_inverse_instance_twiceArthur Nogueira Neves2017-01-032-2/+2
|\ \ | | | | | | Avoid to call `set_inverse_instance` twice for `has_many` association
| * | Avoid to call `set_inverse_instance` twice for `has_many` associationRyuta Kamizono2016-12-282-2/+2
| | | | | | | | | | | | | | | `create`, `create!`, and `concat` in `has_many` association hits `set_inverse_instance` twice. It is enough to hit only once.
* | | Refactor `CollectionAssociation#ids_reader`Ryuta Kamizono2017-01-011-3/+1
| | | | | | | | | | | | | | | Simply we can do `target.pluck(reflection.association_primary_key)` if `target` is loaded.
* | | Remove deprecated force reload argument in association readersRafael Mendonça França2016-12-292-20/+4
| | |
* | | Remove deprecated i18n scopes in Active RecordRafael Mendonça França2016-12-292-16/+2
| | |
* | | Should not update children when the parent creation with no reasonRyuta Kamizono2016-12-291-4/+6
| | | | | | | | | | | | | | | | | | | | | | | | This issue was introduced with d849f42 to solve #19782. However, we can solve #19782 without causing the issue. It is enough to save only when necessary. Fixes #27338.
* | | Fix Rubocop violations and fix documentation visibilityRafael Mendonça França2016-12-282-5/+5
|/ / | | | | | | | | | | Some methods were added to public API in 5b14129d8d4ad302b4e11df6bd5c7891b75f393c and they should be not part of the public API.
* | Merge pull request #27442 from kamipo/fix_27434Eileen M. Uchitelle2016-12-272-13/+16
|\ \ | |/ |/| Add a record to target before any callbacks loads the record
| * Add a record to target before any callbacks loads the recordRyuta Kamizono2016-12-232-13/+16
| | | | | | | | | | | | | | | | | | `append_record` was added at 15ddd51 for not double adding the record. But adding `append_record` (checking `@target.include?(record)`) caused performance regression #27434. Instead of checking not double adding the record, add a record to target before any callbacks loads the record. Fixes #27434.
* | 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.