aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations
Commit message (Collapse)AuthorAgeFilesLines
...
* | | | Keep INNER JOIN when merging relationsMaxime Lapointe2017-06-201-3/+3
|/ / / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Doing `Author.joins(:posts).merge(Post.joins(:comments))` does this `SELECT ... INNER JOIN posts ON... LEFT OUTER JOIN comments ON...` instead of doing `SELECT ... INNER JOIN posts ON... INNER JOIN comments ON...`. This behavior is unexpected and makes little sense as, basically, doing `Post.joins(:comments)` means I want posts that have comments. Turning it to a LEFT JOIN means I want posts and join the comments data, if any. We can see this problem directly in the existing tests. The test_relation_merging_with_merged_joins_as_symbols only does joins from posts to comments to ratings while the ratings fixture isn't loaded, but the count is non-zero.
* | / Prevent extra `scope` construction in `find_target`Ryuta Kamizono2017-06-183-3/+5
| |/ |/| | | | | Because constructing `scope` is a little expensive.
* | Raise on create for singular association when parent is unpersistedAlex Kitchens2017-06-081-0/+4
| | | | | | | | | | | | A collection association will raise on `#create_association` when the parent is unpersisted. A singular association should do the same. This addresses issue #29219.
* | Don't expose methods and attrs for internal usageRyuta Kamizono2017-05-304-8/+10
| |
* | Merge pull request #29098 from kamipo/fix_association_with_extension_issuesMatthew Draper2017-05-304-26/+22
|\ \ | | | | | | | | | Fix association with extension issues
| * | Extract `default_extensions` to avoid `klass.all`Ryuta Kamizono2017-05-301-1/+1
| | | | | | | | | | | | | | | As @matthewd's suggestion, if `klass` has no default scope, it will more faster.
| * | Cache the association proxy objectRyuta Kamizono2017-05-282-4/+8
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Some third party modules expects that association returns same proxy object each time (e.g. for stubbing collection methods: https://github.com/rspec/rspec-rails/issues/1817). So I decided that cache the proxy object and reset scope in the proxy object each time. Related context: https://github.com/rails/rails/commit/c86a32d7451c5d901620ac58630460915292f88b#commitcomment-2784312
| * | Fix association with extension issuesRyuta Kamizono2017-05-283-22/+14
| | | | | | | | | | | | | | | | | | | | | | | | This fixes the following issues. * `association_scope` doesn't include `default_scope`. Should use `scope` instead. * We can't use `method_missing` for customizing existing method. * We can't use `relation_delegate_class` for sharing extensions. Should extend per association.
* | | Remove unused `Association#interpolate`Ryuta Kamizono2017-05-281-8/+0
|/ / | | | | | | Using `Association#interpolate` was removed since #11251.
* | Merge pull request #29174 from kamipo/remove_unused_join_part_nameMatthew Draper2017-05-241-4/+0
|\ \ | | | | | | Remove unused `JoinPart#name`
| * | Remove unused `JoinPart#name`Ryuta Kamizono2017-05-221-4/+0
| |/
* / Refactor making join constraintsRyuta Kamizono2017-05-231-23/+6
|/ | | | | The only difference between `make_inner_joins` and `make_left_outer_joins` is the `join_type`.
* 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