aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/associations/has_many_through_associations_test.rb
Commit message (Collapse)AuthorAgeFilesLines
* Give up filling schema cache before `assert_no_queries`Ryuta Kamizono2019-05-221-8/+2
| | | | | | | | | | | | | | | | | 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)`.
* Implicit through table joins should be appeared before user supplied joinsRyuta Kamizono2019-05-191-0/+8
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | #36293 was an issue for through association with `joins` for a long time, but since #35864 through association with `left_joins` would also be affected by the issue. Implicit through table joins should be appeared before user supplied joins, otherwise loading through association with joins will cause a statement invalid error. Fixes #36293. ``` % ARCONN=postgresql bundle exec ruby -w -Itest test/cases/associations/has_many_through_associations_test .rb -n test_through_association_with_joins Using postgresql Run options: -n test_through_association_with_joins --seed 7116 # Running: E Error: HasManyThroughAssociationsTest#test_through_association_with_joins: ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: missing FROM-clause entry for table "posts" LINE 1: ... "comments_posts" ON "comments_posts"."post_id" = "posts"."i... ^ : SELECT "comments".* FROM "comments" INNER JOIN "comments" "comments_posts" ON "comments_posts"."post_id" = "posts"."id" INNER JOIN "posts" ON "comments"."post_id" = "posts"."id" WHERE "posts"."author_id" = $1 rails test test/cases/associations/has_many_through_associations_test.rb:61 Finished in 0.388657s, 2.5730 runs/s, 0.0000 assertions/s. 1 runs, 0 assertions, 0 failures, 1 errors, 0 skips ```
* Merge pull request #32313 from lulalala/model_error_as_objectRafael França2019-04-241-6/+10
|\ | | | | Model error as object
| * Raise deprecation for calling `[:f] = 'b'` or `[:f] << 'b'`lulalala2019-03-311-6/+10
| | | | | | | | Revert some tests to ensure back compatibility
* | Merge pull request #28155 from lcreid/belongs_toRyuta Kamizono2019-04-101-0/+16
|\ \ | |/ |/| | | Fix "autosave: true" on belongs_to of join model causes invalid records to be saved
| * Fix circular `autosave: true`Larry Reid2018-07-231-0/+16
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Use a variable local to the `save_collection_association` method in `activerecord/lib/active_record/autosave_association.rb`, instead of an instance variable. Prior to this PR, when there was a circular series of `autosave: true` associations, the callback for a `has_many` association was run while another instance of the same callback on the same association hadn't finished running. When control returned to the first instance of the callback, the instance variable had changed, and subsequent associated records weren't saved correctly. Specifically, the ID field for the `belongs_to` corresponding to the `has_many` was `nil`. Remove unnecessary test and comments. Fixes #28080.
* | Fix callbacks on has_many :through associations (#33249)Ryan Kerr2019-03-301-0/+18
| | | | | | | | | | | | | | | | | | | | | | When adding a child record via a has_many :through association, build_through_record would previously build the join record, and then assign the child record and source_type option to it. Because the before_add and after_add callbacks are called as part of build, however, this caused the callbacks to receive incomplete records, specifically without the other end of the has_many :through association. Collecting all attributes before building the join record ensures the callbacks receive the fully constructed record.
* | Fix test that was broken by adding a default scope to an existing modelRafael Mendonça França2019-02-261-2/+2
| |
* | Fix preload with nested associationsRafael Mendonça França2019-02-261-0/+9
| | | | | | | | | | | | When the middle association doesn't have any records and the inner association is not an empty scope the owner will be `nil` so we can't try to reset the inverse association.
* | Fix `CollectionProxy#concat` to return self by alias it to `#<<`Yuya Tanaka2019-02-061-1/+2
| | | | | | | | Formerly it was returning arguments (`records` array).
* | Add regression test for has_many through record creationRyuta Kamizono2019-02-011-0/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | | #33729 affected the behavior of the has_many through record creation. Since #33729, the intermediate reflection of simple has_many through association has `inverse_of` to the association, it causes extra through record creation, the extra through record required valid before the association record is saved. https://github.com/rails/rails/blob/23125378673bcc606b274027666a126573e136f8/activerecord/lib/active_record/associations/has_many_through_association.rb#L95-L102 I think that #33729 need to more work to care about has_many through association, that PR should be reverted to not break existing apps.
* | Enable `Layout/SpaceAfterSemicolon` cop to avoid newly adding odd spacingRyuta Kamizono2018-12-131-3/+3
| | | | | | | | Ref https://github.com/rails/rails/commit/59ff1ba30d9f4d34b4d478104cc3f453e553c67a#diff-38fb97fba84b1ef0f311c4110a597c44R35
* | Merge pull request #34609 from kamipo/delete_all_on_collection_proxyRyuta Kamizono2018-12-041-3/+3
|\ \ | | | | | | | | | Ensure that `delete_all` on collection proxy returns affected count
| * | Ensure that `delete_all` on collection proxy returns affected countRyuta Kamizono2018-12-041-3/+3
| | | | | | | | | | | | | | | | | | | | | 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 handling of duplicates for `replace` on has_many-throughFlorian Ebeling2018-11-061-0/+10
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | There was a bug in the handling of duplicates when assigning (replacing) associated records, which made the result dependent on whether a given record was associated already before being assigned anew. E.g. post.people = [person, person] post.people.count # => 2 while post.people = [person] post.people = [person, person] post.people.count # => 1 This change adds a test to provoke the former incorrect behavior, and fixes it. Cause of the bug was the handling of record collections as sets, and using `-` (difference) and `&` (union) operations on them indiscriminately. This temporary conversion to sets would eliminate duplicates. The fix is to decorate record collections for these operations, and only for the `has_many :through` case. It is done by counting occurrences, and use the record together with the occurrence number as element, in order to make them work well in sets. Given a, b = *Person.all then the collection used for finding the difference or union of records would be internally changed from [a, b, a] to [[a, 1], [b, 1], [a, 2]] for these operations. So a first occurrence and a second occurrence would be distinguishable, which is all that is necessary for this task. Fixes #33942.
* | Call `define_attribute_methods` before `assert_no_queries` to address CI ↵Ryuta Kamizono2018-10-091-0/+6
| | | | | | | | | | | | | | | | | | | | | | | | 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`.
* | Remove `ignore_none: false` to assert no queries more strictlyRyuta Kamizono2018-10-051-1/+1
| | | | | | | | 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.
* | ActiveRecord::Associations::Preloader should preload all instances of the ↵Nikita Sokolov2018-09-161-0/+9
|/ | | | same record
* Don't use `target=`Rafael Mendonça França2018-06-111-1/+1
| | | | | It mark the association as loaded and this can cause the object to be in an stale state.
* Use `-=` to change the update the collection on the associationRafael Mendonça França2018-06-111-1/+1
| | | | | This also mark the association as loaded given we changed it in memory and avoid the next access to the reader to make a query to the databse.
* Fix alias confliction when joining same table on has many through with ↵Ryuta Kamizono2018-06-111-0/+4
| | | | | | | | | | left_joins This regression was caused by #30995 due to `Hash#fetch` won't invoke default proc. Just revert the change since #30995 is completely fixed by e9c1653. Fixes #33048.
* Initialization block is a part of `build_record`Ryuta Kamizono2018-06-041-0/+12
| | | | Should be done before `before_add` callbacks.
* Replace `assert !` with `assert_not`Daniel Colson2018-04-191-1/+1
| | | | | This autocorrects the violations after adding a custom cop in 3305c78dcd.
* Merge pull request #31934 from fatkodima/fix-has_many_through-dumpingRyuta Kamizono2018-02-091-0/+5
|\ | | | | | | Fix marshaling of models with `has_many :through` associations
| * Fix marshaling of models with `has_many :through` associationsfatkodima2018-02-081-0/+6
|/
* Use assert_empty and assert_not_emptyDaniel Colson2018-01-251-9/+9
|
* Use assert_predicate and assert_not_predicateDaniel Colson2018-01-251-16/+16
|
* Don't update counter cache when through record was not destroyedEugene Kenny2018-01-141-0/+10
| | | | | | When removing a record from a has many through association, the counter cache was being updated even if the through record halted the callback chain and prevented itself from being destroyed.
* Merge pull request #23146 from piotrj/issue_18424Ryuta Kamizono2018-01-111-0/+19
|\ | | | | | | When deleting through records, take into account association conditions
| * When deleting through records, take into account association conditionsPiotr Jakubowski2016-05-041-0/+16
| | | | | | | | | | | | | | | | Fixes #18424. When deleting through records, it didn't take into account the conditions that may have been affecting join model table, but was defined in association definition.
* | Fix `stale_state` for nested `has_many :through` associationsRyuta Kamizono2018-01-101-12/+28
| | | | | | | | Need reloading when through record has replaced.
* | Merge pull request #16314 from ↵Ryuta Kamizono2018-01-101-0/+29
|\ \ | | | | | | | | | | | | | | | zoltankiss/allow-nested-has-many-associations-on-unpersisted-parent-instances fix nested `has many :through` associations on unpersisted parent instances
| * | Fix nested `has many :through` associations on unpersisted instancesZoltan Kiss2015-03-261-0/+29
| | | | | | | | | | | | Fixes: #16313
* | | Add test case for `collection_singular_ids` with symbol primary keysRyuta Kamizono2018-01-011-0/+8
| | | | | | | | | | | | This is a regression test for #27864.
* | | Fix all `s/trough/through/`Ryuta Kamizono2017-10-271-1/+1
| | | | | | | | | | | | | | | | | | | | | ``` % git grep -n trough activerecord/test/cases/associations/has_many_through_associations_test.rb:1253: def test_has_many_trough_with_scope_that_has_joined_same_table_with_parent_relation ```
* | | Fix typo `s/trough/through/`Ryuta Kamizono2017-10-271-1/+1
| | |
* | | fix initial countpavel2017-10-271-0/+4
| | |
* | | Joined tables in association scope doesn't use the same aliases with the ↵Ryuta Kamizono2017-10-091-0/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | parent relation's aliases Building association scope in join dependency should respect the parent relation's aliases to avoid using the same alias name more than once. Fixes #30681.
* | | `has_many :through` with unscope should affect to through scopeRyuta Kamizono2017-09-071-0/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | The order of scope evaluation should be from through scope to the association's own scope. Otherwise the association's scope cannot affect to through scope. Fixes #13677. Closes #28449.
* | | Through scope should not be affected by scopingRyuta Kamizono2017-08-151-0/+19
| | | | | | | | | | | | | | | | | | Follow up of #29834. Fixes #30266.
* | | Merge pull request #30169 from awortham/awortham/awortham/fix-sql-distinct-bugRafael Mendonça França2017-08-141-0/+26
|\ \ \ | | | | | | | | | | | | Ensure sum honors distinct on has_many through
| * | | Ensure sum honors distinct on has_many throughAaron Wortham2017-08-141-0/+26
| | | | | | | | | | | | | | | | | | | | | | | | When using a has_many through relation and then summing an attribute the distinct was not being used. This will ensure that when summing an attribute, the number is only used once when distinct has been used.
* | | | Fix `reflection.association_primary_key` for `has_many` associationsRyuta Kamizono2017-08-131-14/+0
|/ / / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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 pull request #29720 from gaurish/ar_find_error_message_improvementRafael França2017-08-111-2/+4
|\ \ \ | | | | | | | | Return Not found Ids in ActiveRecord::NotFound
| * | | Return Not found Ids in ActiveRecord::NotFoundGaurish Sharma2017-07-291-2/+4
| | | | | | | | | | | | | | | | | | | | This builds on top of 15e2da656f41af0124f7577858536f3b65462ad5. now it also returns exact Ids which were not found which will be debugging simple.
* | | | Fix random CI failure due to non-deterministic sorting orderRyuta Kamizono2017-08-081-1/+1
|/ / / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | It should be sorted to be deterministic executed result. ``` % bundle exec rake test_postgresql --verbose TESTOPTS="--seed=52812" (snip) Failure: HasManyThroughAssociationsTest#test_has_many_association_through_a_has_many_association_to_self [/Users/kamipo/src/github.com/rails/rails/activerecord/test/cases/associations/has_many_through_associations_test.rb:820]: --- expected +++ actual @@ -1 +1 @@ -[#<Person id: 85, primary_contact_id: 84, gender: "M", number1_fan_id: 1, lock_version: 0, comments: nil, followers_count: 0, friends_too_count: 0, best_friend_id: nil, best_friend_of_id: nil, insures: 0, born_at: nil, created_at: "2017-08-08 07:33:52", updated_at: "2017-08-08 07:33:52", first_name: "John">, #<Person id: 1, primary_contact_id: 2, gender: "M", number1_fan_id: 3, lock_version: 0, comments: nil, followers_count: 1, friends_too_count: 1, best_friend_id: nil, best_friend_of_id: nil, insures: 0, born_at: nil, created_at: "2017-08-08 07:33:49", updated_at: "2017-08-08 07:33:49", first_name: "Michael">, #<Person id: 3, primary_contact_id: 2, gender: "F", number1_fan_id: 1, lock_version: 0, comments: nil, followers_count: 1, friends_too_count: 1, best_friend_id: nil, best_friend_of_id: nil, insures: 0, born_at: nil, created_at: "2017-08-08 07:33:49", updated_at: "2017-08-08 07:33:49", first_name: "Susan">] +#<ActiveRecord::Associations::CollectionProxy [#<Person id: 1, primary_contact_id: 2, gender: "M", number1_fan_id: 3, lock_version: 0, comments: nil, followers_count: 1, friends_too_count: 1, best_friend_id: nil, best_friend_of_id: nil, insures: 0, born_at: nil, created_at: "2017-08-08 07:33:49", updated_at: "2017-08-08 07:33:49", first_name: "Michael">, #<Person id: 3, primary_contact_id: 2, gender: "F", number1_fan_id: 1, lock_version: 0, comments: nil, followers_count: 1, friends_too_count: 1, best_friend_id: nil, best_friend_of_id: nil, insures: 0, born_at: nil, created_at: "2017-08-08 07:33:49", updated_at: "2017-08-08 07:33:49", first_name: "Susan">, #<Person id: 85, primary_contact_id: 84, gender: "M", number1_fan_id: 1, lock_version: 0, comments: nil, followers_count: 0, friends_too_count: 0, best_friend_id: nil, best_friend_of_id: nil, insures: 0, born_at: nil, created_at: "2017-08-08 07:33:52", updated_at: "2017-08-08 07:33:52", first_name: "John">]> ```
* | | Use frozen-string-literal in ActiveRecordKir Shatrov2017-07-191-0/+2
| | |
* | | Add a test case for overwriting existing condition on associationsRyuta Kamizono2017-07-071-0/+7
| | | | | | | | | | | | | | | | | | | | | | | | Overwriting existing condition on associations has already supported (23bcc65 for eager loading, 2bfa2c0 for preloading). Fixes #27724. Closes #29154.