aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations
Commit message (Collapse)AuthorAgeFilesLines
...
* Remove useless `target_records_from_association`Ryuta Kamizono2017-05-041-11/+3
| | | | Since through association is always loaded by `preloader.preload`.
* Evaluate belongs_to :default option against the owner, not the associationGeorge Claghorn2017-04-271-1/+1
|
* Mixin `CollectionProxy::DelegateExtending` after `ClassSpecificRelation`Ryuta Kamizono2017-04-221-10/+18
| | | | | | | | | | `ClassSpecificRelation` has `method_missing` and the `method_missing` is called first. if an associated class has the missing method in a relation, never reach to the `method_missing` in the `CollectionProxy`. I extracted `DelegateExtending` and included it to the delegate class that including `ClassSpecificRelation` to fix the issue. Fixes https://github.com/rails/rails/pull/28246#issuecomment-296033784.
* Prevent double firing the before save callback of new object when the parent ↵Ryuta Kamizono2017-04-213-46/+34
| | | | | | | | | | | | | | | | | | association saved in the callback Related #18155, #26661, 268a5bb, #27434, #27442, and #28599. Originally #18155 was introduced for preventing double insertion caused by the after save callback. But it was caused the before save issue (#26661). 268a5bb fixed #26661, but it was caused the performance regression (#27434). #27442 added new record to `target` before calling callbacks for fixing #27434. But it was caused double firing before save callback (#28599). We cannot add new object to `target` before saving the object. This is improving #18155 to only track callbacks after `save`. Fixes #28599.
* Remove `CollectionProxy#uniq`Ryuta Kamizono2017-03-231-4/+0
| | | | | Since #28473 `uniq` is delegated to `records`, so `CollectionProxy#uniq` is unnecessary.
* Fix extension method with dirty target in has_many associationsRyuta Kamizono2017-03-201-2/+3
| | | | | | | Extension methods should not delegate to `scope` to respect dirty target on `CollectionProxy`. Fixes #28419.
* Evaluate the default block only when necessaryRyuta Kamizono2017-03-202-3/+3
| | | | Follow up of #28453.
* Add :default option to belongs_to (#28453)George Claghorn2017-03-172-1/+12
| | | | | | | | | | | Use it to specify that an association should be initialized with a particular record before validation. For example: # Before belongs_to :account before_validation -> { self.account ||= Current.account } # After belongs_to :account, default: -> { Current.account }
* 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.