aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/associations/has_many_associations_test.rb
Commit message (Collapse)AuthorAgeFilesLines
* Fix preloading on AR::Relation where records are duplicated by a joinBogdan Gusiev2019-06-061-0/+5
|
* Give up filling schema cache before `assert_no_queries`Ryuta Kamizono2019-05-221-48/+12
| | | | | | | | | | | | | | | | | This reverts commit a1ee4a9ff9d4a3cb255365310ead0dc7b739c6be. Even if a1ee4a9 is applied, CI is still flakiness. https://buildkite.com/rails/rails/builds/61252#2c090afa-aa84-4a2b-8b81-9f09219222c6/994-1005 https://buildkite.com/rails/rails/builds/61252#2e55bf83-1bde-44a2-a4f1-b5c3f6820fb4/929-938 Failing tests by whether schema cache is filled or not, it actually means that whether SCHEMA SQLs are executed or not is not target for the tests. So I've reverted commit a1ee4a9 which filling schema cache before `assert_no_queries`, and replace `assert_no_queries` to `assert_queries(0)`.
* Don't track implicit `touch` mutationRyuta Kamizono2019-05-131-5/+15
| | | | | | | | | | | | | | | This partly reverts the effect of d1107f4d. d1107f4d makes `touch` tracks the mutation whether the `touch` is occurred by explicit or not. Existing apps expects that the previous changes tracks only the changes which is explicit action by users. I'd revert the implicit `touch` mutation tracking since I'd not like to break existing apps. Fixes #36219.
* Fix `automatic_inverse_of` not to be disabled if extension block is givenRyuta Kamizono2019-04-121-8/+8
| | | | | | | | If an association has a scope, `automatic_inverse_of` is to be disabled. But extension block is obviously not a scope. It should not be regarded as a scope. Fixes #28806.
* Merge pull request #35247 from bogdan/fix-source-reflection-reset-codeRyuta Kamizono2019-02-201-3/+5
|\ | | | | Fix reset of the source association when through association is loaded
| * Fix reset of the source association when through association is loadedBogdan Gusiev2019-02-201-3/+5
| | | | | | | | | | | | | | | | | | The special case happens when through association has a custom scope that is applied to the source association when loading. In this case, the soucre association would need to be reset after main association is loaded. See tests. The special case exists when a through association has
* | Revert "Merge pull request #35127 from bogdan/counter-cache-loading"Ryuta Kamizono2019-02-131-36/+14
| | | | | | | | | | | | | | | | | | | | | | | | | | This reverts commit eec3e28a1abf75676dcee58308ee5721bb53c325, reversing changes made to 5588fb4802328a2183f4a55c36d6703ee435f85c. Reason: Marking as loaded without actual loading is too greedy optimization. See more context #35239. Closes #35239. [Edouard CHIN & Ryuta Kamizono]
* | Merge pull request #35178 from bogdan/has-many-sizeRyuta Kamizono2019-02-081-0/+15
|\ \ | |/ |/| Bugfix has_many association #size when ids reader is cached and assoc…
| * Bugfix has_many association #size when ids reader is cached and association ↵Bogdan Gusiev2019-02-081-0/+15
| | | | | | | | is changed
* | Fix `CollectionProxy#concat` to return self by alias it to `#<<`Yuya Tanaka2019-02-061-1/+2
|/ | | | Formerly it was returning arguments (`records` array).
* Merge pull request #35127 from bogdan/counter-cache-loadingRyuta Kamizono2019-02-051-5/+35
|\ | | | | Bugfix association loading behavior when counter cache is zero
| * Bugfix association loading behavior when counter cache is zeroBogdan Gusiev2019-02-051-5/+35
| |
* | Ensure `StatementCache#execute` never raises `RangeError`Ryuta Kamizono2019-01-181-0/+6
| | | | | | | | | | | | | | | | | | | | | | | | | | Since 31ffbf8d, finder methods no longer raise `RangeError`. So `StatementCache#execute` is the only place to raise the exception for finder queries. `StatementCache` is used for simple equality queries in the codebase. This means that if `StatementCache#execute` raises `RangeError`, the result could always be regarded as empty. So `StatementCache#execute` just return nil in that range error case, and treat that as empty in the caller side, then we can avoid catching the exception in much places.
* | Refs #28025 nullify *_type column on polymorphic associations on :nu… ↵Laerti2019-01-151-0/+16
|/ | | | | | (#28078) This PR addresses the issue described in #28025. On `dependent: :nullify` strategy only the foreign key of the relation is nullified. However on polymorphic associations the `*_type` column is not nullified leaving the record with a NULL `*_id` but the `*_type` column is present.
* Merge pull request #34609 from kamipo/delete_all_on_collection_proxyRyuta Kamizono2018-12-041-3/+13
|\ | | | | | | Ensure that `delete_all` on collection proxy returns affected count
| * Ensure that `delete_all` on collection proxy returns affected countRyuta Kamizono2018-12-041-3/+13
| | | | | | | | | | | | | | Unlike the `Relation#delete_all`, `delete_all` on collection proxy doesn't return affected count. Since the `CollectionProxy` is a subclass of the `Relation`, this inconsistency is probably not intended, so it should return the count consistently.
* | Reset scope after collection deleteGannon McGibbon2018-12-041-0/+32
|/ | | | | Reset scope after delete on collection association to clear stale offsets of removed records.
* Fix random CI failure due to non-deterministic sorting orderRyuta Kamizono2018-11-261-2/+2
| | | | https://travis-ci.org/rails/rails/jobs/459534536#L1280
* Merge pull request #34122 from kamipo/generate_relation_methodsRyuta Kamizono2018-10-101-0/+1
|\ | | | | Generate delegation methods to named scope in the definition time
| * Generate delegation methods to named scope in the definition timeRyuta Kamizono2018-10-091-0/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The delegation methods to named scope are defined when `method_missing` is invoked on the relation. Since #29301, the receiver in the named scope is changed to the relation like others (e.g. `default_scope`, etc) for consistency. Most named scopes would be delegated from relation by `method_missing`, since we don't allow scopes to be defined which conflict with instance methods on `Relation` (#31179). But if a named scope is defined with the same name as any method on the `superclass` (e.g. `Kernel.open`), the `method_missing` on the relation is not invoked. To address the issue, make the delegation methods to named scope is generated in the definition time. Fixes #34098.
* | Merge pull request #34094 from ↵Ryuta Kamizono2018-10-101-0/+42
|\ \ | |/ |/| | | | | christophemaximin/fix-activerecord-clearing-of-query-cache Fix inconsistent behavior by clearing QueryCache when reloading associations
| * Clear QueryCache when reloading associationsChristophe Maximin2018-10-101-0/+42
| |
* | Call `define_attribute_methods` before `assert_no_queries` to address CI ↵Ryuta Kamizono2018-10-091-6/+43
| | | | | | | | | | | | | | | | | | | | | | | | flakiness Follow up 45be690f8e6db019aac6198ba49d608a2e14824b. Somehow calling `define_attribute_methods` in `build`/`new` sometimes causes the `table_exists?` query. To address CI flakiness due to `assert_no_queries` failure, ensure `define_attribute_methods` before `assert_no_queries`.
* | Fix test name to add missing "set"Ryuta Kamizono2018-10-081-1/+1
| |
* | Fix `AssociationRelation` not to set inverse instance key just like beforeRyuta Kamizono2018-10-071-0/+12
|/ | | | | | | | | | | | | | | | | Since #31575, `set_inverse_instance` replaces the foreign key by the current owner immediately to make it happen when a record is added to collection association. But `set_inverse_instance` is not only called when a record is added, but also when a record is loaded from queries. And also, that loaded records are not always associated records for some reason (using `or`, `unscope`, `rewhere`, etc). It is hard to distinguish whether or not we should invoke `set_inverse_instance`, but at least we should avoid the undesired side-effect which was brought from #31575. Fixes #34108.
* Remove `ignore_none: false` to assert no queries more strictlyRyuta Kamizono2018-10-051-12/+12
| | | | Follow up 811be477786455d144819a5e9fbb7f9f54b8da69.
* Use `assert_no_queries` not to ignore BEGIN/COMMIT queriesRyuta Kamizono2018-10-051-6/+6
| | | | | | | | | `test_update_does_not_run_sql_if_record_has_not_changed` would pass without #18501 since `assert_queries` ignores BEGIN/COMMIT unless `ignore_none: true` is given. Since #32647, empty BEGIN/COMMIT is ommited. So we no longer need to use `assert_queries(0)` to ignore BEGIN/COMMIT in the queries.
* Revert "Remove `counter_cache_target` which is no longer called"Ryuta Kamizono2018-09-261-1/+1
| | | | | | | | This reverts commit 376ffe0ea2e59dc51461122210729c05a10fb443. Since 38fae1f, `association.increment_counters` is called without inflated parent target if inverse_of is disabled. In that case, that commit would cause extra queries to inflate parent.
* Update counter cache in memory if parent target is existedRyuta Kamizono2018-09-261-0/+32
| | | | Fixes #19550.
* Remove unused requiresRyuta Kamizono2018-08-251-3/+1
|
* Add test case to test enum in has_manyRich2018-08-251-0/+22
| | | | | | There is test in has_one to test enum, but there is no for has_many. [Rich Chen]
* Add method_call_assertions and use them instead of Mochautilum2018-08-131-12/+20
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Six Mocha calls prove quite resistant to Minitestification. For example, if we replace ``` ActiveRecord::Associations::HasManyAssociation .any_instance .expects(:reader) .never ``` with `assert_not_called`, Minitest wisely raises ``` NameError: undefined method `reader' for class `ActiveRecord::Associations::HasManyAssociation' ``` as `:reader` comes from a deeply embedded abstract class, `ActiveRecord::Associations::CollectionAssociation`. This patch tackles this difficulty by adding `ActiveSupport::Testing::MethodCallAsserts#assert_called_on_instance_of` which injects a stubbed method into `klass`, and verifies the number of times it is called, similar to `assert_called`. It also adds a convenience method, `assert_not_called_on_instance_of`, mirroring `assert_not_called`. It uses the new method_call_assertions to replace the remaining Mocha calls in `ActiveRecord` tests. [utilum + bogdanvlviv + kspath]
* Fix `collection.create` to could be rolled back by `after_save`Ryuta Kamizono2018-06-071-0/+11
| | | | | | | | | | | In `_create_record`, explicit `transaction` block requires rollback handling manually when `insert_record` is failed. We need to handle it in `_create_record`, not in `insert_record`, since our test cases expect a record added to target and returned even if `insert_record` is failed, Closes #31488.
* Fix `CustomCops/AssertNot` to allow it to have failure messageRyuta Kamizono2018-05-131-1/+1
| | | | Follow up of #32605.
* Merge pull request #32727 from utilum/assert_dont_expectsRafael França2018-04-271-9/+13
|\ | | | | Use MethodCallAssertions instead of mocha expects
| * assert_calledutilum2018-04-261-3/+4
| |
| * assert_not_calledutilum2018-04-261-6/+9
| |
* | Ensure that `ids_reader` respects dirty target whether target is loaded or notRyuta Kamizono2018-04-271-2/+2
| | | | | | | | | | | | | | Currently `ids_reader` doesn't respect dirty target when the target is not loaded yet unlike `collection.size`. I believe the inconsistency is a bug, fixes the `ids_reader` to behave consistently regardless of whether target is loaded or not.
* | Loaded associations should not run a new query when size is calledGraham Turner2018-04-261-0/+74
|/ | | | | | | Already loaded associations were running an extra query when `size` was called on the association. This fix ensures that an extra query is no longer run. Update tests to use proper methods
* Add test case for `collection.size` with dirty targetRyuta Kamizono2018-04-211-0/+10
|
* Replace `assert !` with `assert_not`Daniel Colson2018-04-191-2/+2
| | | | | This autocorrects the violations after adding a custom cop in 3305c78dcd.
* Fix intermittent CI failure due to setting explicit `person.id = 10`Ryuta Kamizono2018-03-301-4/+0
| | | | https://travis-ci.org/rails/rails/jobs/360109429
* Association scope's own order should be prioritized over through scope's orderRyuta Kamizono2018-02-181-0/+7
| | | | | | | | | | 3acc5d6 was changed the order of scope evaluation from through scope to the association's own scope to be prioritized over the through scope. But the sorting order will be prioritized that is evaluated first. It is unintentional effect, association scope's sorting order should be prioritized as well. Fixes #32008.
* Deprecate update_attributes and update_attributes!Eddie Lebow2018-02-171-6/+6
| | | | Closes #31998
* Remove extra whitespaceDaniel Colson2018-01-251-14/+14
|
* Use assert_empty and assert_not_emptyDaniel Colson2018-01-251-10/+10
|
* Use assert_predicate and assert_not_predicateDaniel Colson2018-01-251-64/+64
|
* Merge pull request #27561 from fishbrain/count-all-in-has-many-associationRyuta Kamizono2018-01-031-0/+8
|\ | | | | | | Use `count(:all)` in HasManyAssociation#count_records
| * Use `count(:all)` in HasManyAssociation#count_recordsKlas Eskilson2017-02-071-0/+8
| | | | | | | | | | | | | | | | | | | | | | | | Problem: Calling `count` on an association can cause invalid SQL queries to be created where the `SELECT COUNT(a, b, c)` function receives multiple columns. This will cause a `StatementInvalid` exception later on. Solution: Use `count(:all)`, which generates a `SELECT COUNT(*)...` query independently of the association. This also includes a test case that, before the fix, broke.
* | Bugfix foreign key replacement in inverse associationBogdan Gusiev2017-12-271-0/+9
| | | | | | | | when model is added to collection association