aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/associations/has_many_associations_test.rb
Commit message (Collapse)AuthorAgeFilesLines
* 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
* | Enable `Layout/SpaceBeforeComma` rubocop rule, and fixed moreRyuta Kamizono2017-12-121-1/+1
| | | | | | | | Follow up of #31390.
* | Set counter caches to correct values in fixturesBogdan Gusiev2017-11-081-1/+1
| |
* | Add deterministic sorting order for `test_taking_with_a_number`Ryuta Kamizono2017-08-181-1/+9
| | | | | | | | | | | | Otherwise random CI failure will be caused. https://travis-ci.org/rails/rails/jobs/265848046#L777
* | Fix `reflection.association_primary_key` for `has_many` associationsRyuta Kamizono2017-08-131-3/+14
| | | | | | | | | | | | | | | | | | | | | | | | | | | | 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.
* | Merge remote-tracking branch 'origin/master' into unlock-minitestRafael Mendonça França2017-08-011-30/+29
|\ \
| * | Use frozen-string-literal in ActiveRecordKir Shatrov2017-07-191-0/+2
| | |
| * | Remove extra `.merge!(order: "id")` for `Relation#first` in testsRyuta Kamizono2017-07-131-30/+27
| | | | | | | | | | | | | | | Since 07e5301, `Relation#first` will order by primary key if no order is defined.
* | | Merge branch 'master' into unlock-minitestKasper Timm Hansen2017-07-151-2/+57
|\| |
| * | 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
| | | |
| * | | Merge pull request #29631 from kamipo/should_be_clear_association_idsMatthew Draper2017-07-011-0/+5
| |\ \ \ | | | | | | | | | | Should be clear `@association_ids` when joined newly associated record
| | * | | Should be clear `@association_ids` when joined newly associated recordRyuta Kamizono2017-06-301-0/+5
| | |/ / | | | | | | | | | | | | Fixes #29627.
| * | | Merge pull request #28808 from fschwahn/fix-polymorphic-automic-inverseMatthew Draper2017-07-011-0/+14
| |\ \ \ | | |/ / | |/| | Fix automatic inverse for polymorphic interfaces
| | * | Add regression test for setting inverse instances on normal & polymorphic ↵Fabian Schwahn2017-04-201-0/+14
| | | | | | | | | | | | | | | | relationships when building objects on new records
| * | | Merge pull request #29611 from kamipo/add_test_case_for_unscoping_default_scopeRafael França2017-06-281-2/+3
| |\ \ \ | | | | | | | | | | Add a test case for unscoping `default_scope` in associations
| | * | | Add a test case for unscoping `default_scope` in associationsRyuta Kamizono2017-06-291-2/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Unscoping `default_scope` in associations has already supported (#17360 for preloading, c9cf8b8 for eager loading). Fixes #20679. Closes #16531.
| * | | | Use `reload` in `test_find_first_after_reload`Ryuta Kamizono2017-06-281-10/+10
| |/ / / | | | | | | | | | | | | | | | | | | | | | | | | And use `assert_same` instead of `assert_equal` and tiny fix assert message s/#reload/#reset/. Follow up of #29511.
| * | | Move clearing of @offsets cache to reset_scopeJohn Hawthorn2017-06-211-12/+21
| | | |
| * | | Clear offset cache on CollectionProxy reset/reloadJohn Hawthorn2017-06-201-0/+26
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The `@offsets` cache is used by FinderMethods to cache records found by find_nth. This cache is cleared in AR::Relation#reset, but not in CollectionProxy#reset or CollectionProxy#reload. Because of this, since #29098, calling #first/#find_nth/etc after calling #reload or #reset on an association could return a stale record. This is an issue both when the full association target is loaded and when the item is loaded in #find_nth. This commit solves the problem by clearing the `@offsets` cache in CollectionProxy#reset and CollectionProxy#reload.
* | | | Merge branch 'master' into unlock-minitestRafael Mendonça França2017-06-021-2/+10
|\| | |
| * | | Correct a has_many association testKoichi ITO2017-06-011-1/+1
| | | |
| * | | Fix association with extension issuesRyuta Kamizono2017-05-281-1/+9
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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.
* | | | Merge branch 'master' into unlock-minitestKasper Timm Hansen2017-05-291-3/+3
|\| | |
| * | | Restore `fixtures :author_addresses`Ryuta Kamizono2017-04-271-3/+3
| | | | | | | | | | | | | | | | | | | | This change reverted in eac6f369 but it is needed for data integrity. See #25328.
* | | | Fix the random failure of `test_create_resets_cached_counters`Prathamesh Sonpatki2017-04-261-0/+3
|/ / / | | | | | | | | | | | | | | | | | | | | | - Tried specifying `id` for the `readers` records but it is interconnected with so many tests that many random tests started failing. - So switched to the approach of deleting all readers in the `create_resets_cached_counters` test.
* | | Merge pull request #28885 from kamipo/remove_useless_test_caseRafael França2017-04-261-5/+0
|\ \ \ | | | | | | | | Remove useless test case