aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations/collection_association.rb
Commit message (Collapse)AuthorAgeFilesLines
* Merge pull request #27609 from kamipo/fix_association_primary_keyRafael França2017-08-141-4/+7
|\ | | | | Fix `reflection.association_primary_key` for `has_many` association
| * Fix `reflection.association_primary_key` for `has_many` associationsRyuta Kamizono2017-08-131-4/+7
| | | | | | | | | | | | | | | | | | | | | | | | | | | | It is incorrect to treat `options[:primary_key]` as `association_primary_key` if `has_many` associations because the `:primary_key` means the column on the owner record, not on the association record. It will break `ids_reader` and `ids_writer`. ```ruby people(:david).essay_ids # => ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column 'essays.first_name' in 'field list': SELECT `essays`.first_name FROM `essays` WHERE `essays`.`writer_id` = 'David' ``` Fixes #14439.
* | Delegate to `Enumerable#find` for `CollectionProxy`Ryuta Kamizono2017-08-131-15/+11
|/ | | | | Since `Relation` includes `Enumerable`, it is enough to use `super` simply.
* Merge pull request #29720 from gaurish/ar_find_error_message_improvementRafael França2017-08-111-1/+3
|\ | | | | Return Not found Ids in ActiveRecord::NotFound
| * Return Not found Ids in ActiveRecord::NotFoundGaurish Sharma2017-07-291-1/+3
| | | | | | | | | | This builds on top of 15e2da656f41af0124f7577858536f3b65462ad5. now it also returns exact Ids which were not found which will be debugging simple.
* | Passing `klass` to `StatementCache.new`Ryuta Kamizono2017-08-041-6/+4
|/ | | | | | Actually `StatementCache#execute` is always passed the same klass that the owner klass of the connection when the statement cache is created. So passing `klass` to `StatementCache.new` will make more DRY.
* Use frozen-string-literal in ActiveRecordKir Shatrov2017-07-191-0/+2
|
* Fix `create_with` using both string and symbolRyuta Kamizono2017-07-161-4/+0
| | | | | | | This is related with #27680. Since `where_values_hash` keys constructed by `where` are string, so we need `stringify_keys` to `create_with_value` before merging it.
* Revert "Merge pull request #29540 from kirs/rubocop-frozen-string"Matthew Draper2017-07-021-1/+0
| | | | | This reverts commit 3420a14590c0e6915d8b6c242887f74adb4120f9, reversing changes made to afb66a5a598ce4ac74ad84b125a5abf046dcf5aa.
* Merge pull request #29540 from kirs/rubocop-frozen-stringMatthew Draper2017-07-021-0/+1
|\ | | | | | | Enforce frozen string in Rubocop
| * Enforce frozen string in RubocopKir Shatrov2017-07-011-0/+1
| |
* | Should be clear `@association_ids` when joined newly associated recordRyuta Kamizono2017-06-301-2/+11
|/ | | | Fixes #29627.
* Merge pull request #29610 from ↵Rafael França2017-06-281-1/+1
|\ | | | | | | | | kamipo/dont_passing_klass_connection_to_association_scope Don't passing `klass.connection` to `AssociationScope`
| * Don't passing `klass.connection` to `AssociationScope`Ryuta Kamizono2017-06-291-1/+1
| | | | | | | | | | Passing `klass.connection` is redundant because `AssociationScope` is passed an association itself and an association has `klass`.
* | Fix `ids_reader` to respect case sensitive primary keyRyuta Kamizono2017-06-281-4/+1
|/ | | | | | | | | | | | ```ruby car = Car.create!(name: "Tofaş") # Before car.bulb_ids # => SELECT "bulbs".ID FROM "bulbs" WHERE "bulbs"."name" = $1 AND "bulbs"."car_id" = $2 [["name", "defaulty"], ["car_id", 3]] # After car.bulb_ids # => SELECT "bulbs"."ID" FROM "bulbs" WHERE "bulbs"."name" = $1 AND "bulbs"."car_id" = $2 [["name", "defaulty"], ["car_id", 3]] ```
* Prevent extra `scope` construction in `find_target`Ryuta Kamizono2017-06-181-1/+2
| | | | Because constructing `scope` is a little expensive.
* Cache the association proxy objectRyuta Kamizono2017-05-281-1/+2
| | | | | | | | | | 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
* Prevent double firing the before save callback of new object when the parent ↵Ryuta Kamizono2017-04-211-35/+32
| | | | | | | | | | | | | | | | | | 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.
* Merge pull request #25877 from kamipo/delegate_to_scope_rather_than_mergeMatthew Draper2017-02-211-10/+4
|\ | | | | Delegate to `scope` rather than `merge!` for collection proxy
| * 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-251-3/+3
| | | | | | | | | | `merge! association.scope(nullify: false)` is expensive but most methods do not need the merge.
* | Merge pull request #26352 from kamipo/avoid_to_call_set_inverse_instance_twiceArthur Nogueira Neves2017-01-031-1/+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-281-1/+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-291-10/+2
|/ /
* / Add a record to target before any callbacks loads the recordRyuta Kamizono2016-12-231-9/+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.
* 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
* Merge pull request #26980 from ↵Sean Griffin2016-11-171-7/+0
|\ | | | | | | | | kamipo/respect_new_records_for_collection_proxy_distinct Respect new records for `CollectionProxy#uniq`
| * Respect new records for `CollectionProxy#uniq`Ryuta Kamizono2016-11-131-7/+0
| | | | | | | | | | | | | | | | | | | | | | | | | | 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-131-2/+3
|\ \ | |/ |/| Add missing `+` around a some literals.
| * Add missing `+` around a some literals.bogdanvlviv2016-10-271-2/+3
| | | | | | | | | | | | Mainly around `nil` [ci skip]
* | 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.
* / 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.