aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/associations/belongs_to_associations_test.rb
Commit message (Collapse)AuthorAgeFilesLines
* Use capture_sql helper method in testsst00122019-07-281-4/+1
|
* Use match? where we don't need MatchDataAkira Matsuda2019-07-271-1/+1
| | | | We're already running Performance/RegexpMatch cop, but it seems like the cop is not always =~ justice
* Fix `pluck` and `select` with custom attributesRyuta Kamizono2019-02-131-2/+7
| | | | | | | | | Currently custom attributes are always qualified by the table name in the generated SQL wrongly even if the table doesn't have the named column, it would cause an invalid SQL error. Custom attributes should only be qualified if the table has the same named column.
* Address `test_belongs_to_does_not_use_order_by` failure due to checking ↵Yasuo Honda2019-02-081-1/+2
| | | | | | | | order by for metadata queries Also, `test_has_one_does_not_use_order_by` should not check metadata queries. Fixes #35098
* Ensure `StatementCache#execute` never raises `RangeError`Ryuta Kamizono2019-01-181-7/+7
| | | | | | | | | | | | | 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.
* No need to handle if FrozenError is availableYasuo Honda2018-12-231-2/+2
| | | | | | | Rails 6 requires Ruby 2.5, which introduces `FrozenError` https://docs.ruby-lang.org/en/2.5.0/NEWS.html Related to #31520
* More exercise singular association queryRyuta Kamizono2018-11-281-3/+3
| | | | Follow up ba4e68f577efc76f351d30a2914e29942b97830e.
* Ensure that singular association should execute limited queryRyuta Kamizono2018-11-281-3/+6
|
* Clear QueryCache when reloading associationsChristophe Maximin2018-10-101-0/+24
|
* Use `assert_no_queries` not to ignore BEGIN/COMMIT queriesRyuta Kamizono2018-10-051-2/+2
| | | | | | | | | `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.
* Avoid extra touch queries when counter cache is updatedRyuta Kamizono2018-09-271-4/+8
| | | | Since counter cache handles touch option too.
* Remove force parent loading when counter cache child is created/destroyedRyuta Kamizono2018-09-261-1/+5
| | | | | | | `association.increment_counters` and `association.decrement_counters` works regardless of parent target is loaded or not. Related 52e11e462f6114a4d12225c639c5f501f0ffec7a.
* Don't update counter cache unless the record is actually savedRyuta Kamizono2018-09-191-6/+36
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This is a 4th attempt to make counter cache transactional completely. Past attempts: #9236, #14849, #23357. All existing counter cache issues (increment/decrement twice, lost increment) are caused due to updating counter cache on the outside of the record saving transaction by assigning belongs_to record, even though assigning that doesn't cause the record saving. We have the `@_after_replace_counter_called` guard condition to mitigate double increment/decrement issues, but we can't completely prevent that inconsistency as long as updating counter cache on the outside of the transaction, since saving the record is not always happened after that. We already have handling counter cache after create/update/destroy, https://github.com/rails/rails/blob/1b90f614b1b3d06b7f02a8b9ea6cd84f15d58643/activerecord/lib/active_record/counter_cache.rb#L162-L189 https://github.com/rails/rails/blob/1b90f614b1b3d06b7f02a8b9ea6cd84f15d58643/activerecord/lib/active_record/associations/builder/belongs_to.rb#L33-L59 so just removing assigning logic on the belongs_to association makes counter cache transactional completely. Closes #14849. Closes #23357. Closes #31493. Closes #31494. Closes #32372. Closes #33113. Closes #33117 Closes #33129. Closes #33458.
* Avoid the same `foreign_key` and `counter_cache` associations on `SillyReply`Ryuta Kamizono2018-09-191-0/+1
| | | | | | | | | `topic` and `reply` belongs_to associations on `SillyReply` are defined with the same `foreign_key` (`parent_id`) and `counter_cache` (`replies_count`) columns. This would cause unintentional side-effect (e.g. saving `SillyReply` object would cause double increment `replies_count`), so it is better to avoid that side-effect.
* Fix that association's after_touch is not called with counter cacheRyuta Kamizono2018-05-271-0/+17
| | | | | | | | | | | | Since #31405, using `#increment!` with touch option instead of `#touch` to touch belongs_to association if counter cache is enabled. It caused the regression since `#increment!` won't invoke after_touch callbacks even if touch option is given. To fix the regression, make `#increment!` invokes after_touch callbacks if touch option is given. Fixes #31559. Fixes #32408.
* Fix inconsistent touching behavior between assigning and unassigningRyuta Kamizono2018-05-271-0/+25
| | | | | | | | | | | On belongs_to with `touch: true` association, unassigned object is caused touching, but assigned object is not touched. And also, if primary key is customized, it will touch against the wrong target looked up by the customized key as primary key. This change ensures correctly touching consistently between assigning and unassigning.
* Fix `belongs_to_counter_cache_after_update` to respect polymorphic type changeRyuta Kamizono2018-05-271-1/+12
|
* Fix `belongs_to_counter_cache_after_update` to respect custom primary key ↵Ryuta Kamizono2018-05-261-1/+7
| | | | | | | | | | counter If belongs_to primary key is customized, the callback will update counters against the wrong target looked up by the customized key as primary key. We need to convert the customized key into an object that can be referred to as primary key.
* Fix `different_target?` to respect custom primary key counterRyuta Kamizono2018-05-261-0/+7
|
* Eager loading won't mutate owner recordRyuta Kamizono2018-05-251-0/+8
| | | | | | | | | | | | | | | Since #31575, `BelongsToAssociation#target=` replaces owner record's foreign key to fix an inverse association bug. But the method is not only used for inverse association but also used for eager loading/preloading, it caused some public behavior changes (#32338, #32375). To avoid any side-effect in loading associations, I reverted the overriding `#target=`, then introduced `#inversed_from` to replace foreign key in `set_inverse_instance`. Closes #32375.
* Allow a belonging to object to be created from a new recordJolyon Pawlyn2018-05-011-0/+9
| | | | If a 'has one' object is created from a new record, an ActiveRecord::RecordNotSaved error is raised but this behavior was also applied to the reverse scenario.
* Add test case that assigning belongs_to on destroyed object raises frozen errorRyuta Kamizono2018-04-271-0/+7
| | | | | This is to ensure that the behavior has not changed before and after #31575.
* Fix dependence on has_one/belongs_to relationshipsFernando Gorodscy2018-03-061-0/+24
| | | | | | | | | | | | | | | | | | | | | | | | | | When a class has a belongs_to or has_one relationship with dependent: :destroy option enabled, objects of this class should not be deleted if it's dependents cannot be deleted. Example: class Parent has_one :child, dependent: :destroy end class Child belongs_to :parent, inverse_of: :child before_destroy { throw :abort } end c = Child.create p = Parent.create(child: c) p.destroy p.destroyed? # expected: false; actual: true; Fixes #32022
* Use assert_predicate and assert_not_predicateDaniel Colson2018-01-251-24/+24
|
* Fix `warning: assigned but unused variable - comment`Ryuta Kamizono2017-07-251-1/+1
| | | | | | | | | | | | | | | ``` % ARCONN=sqlite3 be ruby -w -Itest test/cases/associations/belongs_to_associations_test.rb -n test_multiple_counter_cache_with_after_create_update test/cases/associations/belongs_to_associations_test.rb:1181: warning: assigned but unused variable - comment Using sqlite3 Run options: -n test_multiple_counter_cache_with_after_create_update --seed 49644 . Finished in 0.114266s, 8.7515 runs/s, 17.5030 assertions/s. 1 runs, 2 assertions, 0 failures, 0 errors, 0 skips ```
* Merge pull request #29765 from lugray/fix_counter_cacheRafael França2017-07-241-0/+11
|\ | | | | Fix `counter_cache` double increment
| * Add test for fixed `counter_cache` double incrementLisa Ugray2017-07-191-0/+11
| | | | | | | | | | | | | | | | | | | | | | When an `after_create` callback did `update_attributes` on a record with multiple `belongs_to` associations with counter caches, even numbered associations would have their counters double-incremented. Fixes to `ActiveModel::Dirty` in 020abad fixed this. This adds regression tests for this bug fixed incidentally in the other commit, which also removed the need for the workaround using @_after_create_counter_called.
* | Reset column information after schema changedyuuji.yaginuma2017-07-201-0/+2
| | | | | | | | | | This fixes the following failures. https://travis-ci.org/rails/rails/jobs/253990014
* | Use frozen-string-literal in ActiveRecordKir Shatrov2017-07-191-0/+2
|/
* Remove extra `.merge!(order: "id")` for `Relation#first` in testsRyuta Kamizono2017-07-131-2/+1
| | | | | Since 07e5301, `Relation#first` will order by primary key if no order is defined.
* 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.
* Enforce frozen string in RubocopKir Shatrov2017-07-011-0/+1
|
* Remove unused `aliased_table_name` in `Association`Ryuta Kamizono2017-06-291-1/+0
| | | | | | | `aliased_table_name` in `Association` was added at a3502c4. `aliased_table_name` in `JoinDependency` (added at 55854c4) is used, but it looks like that added one in `Association` is never used from the beginning.
* Evaluate belongs_to :default option against the owner, not the associationGeorge Claghorn2017-04-271-0/+18
|
* Add :default option to belongs_to (#28453)George Claghorn2017-03-171-0/+20
| | | | | | | | | | | 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 }
* Remove deprecated force reload argument in association readersRafael Mendonça França2016-12-291-6/+0
|
* "Use assert_nil if expecting nil. This will fail in minitest 6."Akira Matsuda2016-12-251-1/+1
|
* Fix CI failure caused by #25227 and #25280 were merged at the same timeRyuta Kamizono2016-12-101-1/+1
|
* Merge pull request #25280 from ↵Sean Griffin2016-12-101-0/+14
|\ | | | | | | | | kamipo/prevent_range_error_for_belongs_to_associations Prevent `RangeError` for `belongs_to` associations
| * Prevent `RangeError` for `belongs_to` associationsRyuta Kamizono2016-10-101-0/+14
| | | | | | | | | | | | | | | | Currently to access `belongs_to` associations raises a `RangeError` if foreign key attribute has out of range value. It should return a nil value rather than raising a `RangeError`. Fixes #20140.
* | Introduce `reload_<association>` reader for singular associations.Yves Senn2016-11-221-0/+10
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This patch brings back the functionality of passing true to the association proxy. The behavior was deprecated with #20888 and scheduled for removal in Rails 5.1. The deprecation mentioned that instead of `Article.category(true)` one should use `article#reload.category`. Unfortunately the alternative does not expose the same behavior as passing true to the reader did. Specifically reloading the parent record throws unsaved changes and other caches away. Passing true only affected the association. This is problematic and there is no easy workaround. I propose to bring back the old functionality by introducing this new reader method for singular associations.
* | Add more rubocop rules about whitespacesRafael Mendonça França2016-10-291-2/+2
|/
* Don't unnecessarily load a belongs_to when saving.James Coleman2016-08-261-0/+6
| | | | | | | | Previously, if the the association was previously loaded and then the foreign key changed by itself, a #save call would trigger a load of the new associated record during autosave. This is unnecessary and the autosave code (in that case) didn't use the loaded record anyways.
* Add three new rubocop rulesRafael Mendonça França2016-08-161-7/+7
| | | | | | | | Style/SpaceBeforeBlockBraces Style/SpaceInsideBlockBraces Style/SpaceInsideHashLiteralBraces Fix all violations in the repository.
* modernizes hash syntax in activerecordXavier Noria2016-08-061-51/+51
|
* applies new string literal convention in activerecord/testXavier Noria2016-08-061-51/+51
| | | | | The current code base is not uniform. After some discussion, we have chosen to go with double quotes by default.
* Give more context from `AssociationMismatchError`Sean Griffin2016-05-121-1/+1
| | | | | | | The error message that we give today makes this error difficult to debug if you receive it. I have no clue why we're printing the object ID of the class (the commit doesn't give context), but I've left it as it was deliberate.
* Fix counter_cache double increment bugTom Kadwill2016-04-281-0/+11
|
* Remove duplicated `test_` prefix [ci skip]Ryuta Kamizono2016-03-021-1/+1
|
* Remove legacy mysql adapterAbdelkader Boudih2015-12-171-1/+1
|