| Commit message (Collapse) | Author | Age | Files | Lines |
|
|
|
|
|
|
|
|
|
|
| |
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 preloader to never reset associations in case they are already loaded
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| | |
This patch fixes the issue when association is preloaded with a custom
preload scope which disposes the already preloaded target of the
association by reseting it.
When custom preload scope is used, the preloading is now performed into
a separated Hash - #records_by_owner instead of the association.
It removes the necessaty the reset the association after the preloading
is complete so that reset of the preloaded association never happens.
Preloading is still happening to the association when the preload scope
is empty.
|
| |
| |
| |
| |
| |
| |
| |
| |
| | |
`TRUNCATE TABLE posts` also resets `AUTO_INCREMENT`. If newly created a
post, it is wrongly associated with remaining tagging records.
To un-associate remaining tagging record, use `post.create_tagging!`
instead.
Fixes #35751.
|
|/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
queries
Fix #35665
```ruby
$ ARCONN=mysql2 bin/test test/cases/scoping/named_scoping_test.rb test/cases/tasks/database_tasks_test.rb test/cases/associations/cascaded_eager_loading_test.rb test/cases/associations/eager_singularization_test.rb -n "/^(?:NamedScopingTest#(?:test_many_should_not_fire_query_if_scope_loaded)|ActiveRecord::DatabaseTasksDumpSchemaCacheTest#(?:test_dump_schema_cache)|CascadedEagerLoadingTest#(?:test_eager_association_loading_with_has_many_sti_and_subclasses)|EagerSingularizationTest#(?:test_eager_no_extra_singularization_has_many_through_belongs_to))$/" --seed 16818
Using mysql2
Run options: -n "/^(?:NamedScopingTest#(?:test_many_should_not_fire_query_if_scope_loaded)|ActiveRecord::DatabaseTasksDumpSchemaCacheTest#(?:test_dump_schema_cache)|CascadedEagerLoadingTest#(?:test_eager_association_loading_with_has_many_sti_and_subclasses)|EagerSingularizationTest#(?:test_eager_no_extra_singularization_has_many_through_belongs_to))$/" --seed 16818
...F
Failure:
CascadedEagerLoadingTest#test_eager_association_loading_with_has_many_sti_and_subclasses [/home/yahonda/git/rails/activerecord/test/cases/associations/cascaded_eager_loading_test.rb:124]:
1 instead of 0 queries were executed.
Queries:
SHOW FULL FIELDS FROM `topics`.
Expected: 0
Actual: 1
bin/test test/cases/associations/cascaded_eager_loading_test.rb:119
Finished in 6.894609s, 0.5802 runs/s, 1.0153 assertions/s.
4 runs, 7 assertions, 1 failures, 0 errors, 0 skips
$
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
Follow up #35455, there are two more test cases unnecessary `if current_adapter?(:OracleAdapter)`
```ruby
$ ARCONN=oracle bin/test test/cases/associations/eager_test.rb -n test_include_has_many_using_primary_key
Using oracle
Run options: -n test_include_has_many_using_primary_key --seed 62842
.
Finished in 50.280024s, 0.0199 runs/s, 0.0398 assertions/s.
1 runs, 2 assertions, 0 failures, 0 errors, 0 skips
$
```
```
$ ARCONN=oracle bin/test test/cases/migration/index_test.rb -n test_add_index
Using oracle
Run options: -n test_add_index --seed 52034
.
Finished in 13.152620s, 0.0760 runs/s, 0.0000 assertions/s.
1 runs, 0 assertions, 0 failures, 0 errors, 0 skips
$
```
|
| |
|
|
|
|
|
|
| |
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 reset of the source association when through association is loaded
|
| |
| |
| |
| |
| |
| |
| |
| |
| | |
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
|
| |
| |
| |
| |
| |
| | |
This fixes a bug that the `foreign_key` and the `foreign_type` are
separated as different table conditions if a polymorphic association has
a scope that joins another tables.
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| | |
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]
|
| |
| |
| |
| |
| |
| |
| |
| |
| | |
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.
|
| |
| |
| |
| | |
https://travis-ci.org/rails/rails/jobs/491045821#L1528-L1531
|
|\ \
| |/
|/| |
Bugfix has_many association #size when ids reader is cached and assoc…
|
| |
| |
| |
| | |
is changed
|
| |
| |
| |
| |
| |
| |
| |
| | |
order by for metadata queries
Also, `test_has_one_does_not_use_order_by` should not check metadata queries.
Fixes #35098
|
|/
|
|
| |
Formerly it was returning arguments (`records` array).
|
|\
| |
| | |
Bugfix association loading behavior when counter cache is zero
|
| | |
|
| |
| |
| |
| |
| |
| |
| | |
This reverts commit ed1eda271c7ac82ecb7bd94b6fa1b0093e648a3e, reversing
changes made to 3d2caab7dc92a13d4dd369678d5b4ce659df8e52.
Reason: 7c3da6e0030aa080fcb89af58b094ed50d861a44
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| | |
#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.
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| | |
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.
|
|/
|
|
|
|
| |
(#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.
|
|
|
|
|
|
|
| |
Rails 6 requires Ruby 2.5, which introduces `FrozenError`
https://docs.ruby-lang.org/en/2.5.0/NEWS.html
Related to #31520
|
|
|
|
|
|
|
|
|
|
| |
Currently we sometimes find a redundant begin block in code review
(e.g. https://github.com/rails/rails/pull/33604#discussion_r209784205).
I'd like to enable `Style/RedundantBegin` cop to avoid that, since
rescue/else/ensure are allowed inside do/end blocks in Ruby 2.5
(https://bugs.ruby-lang.org/issues/12906), so we'd probably meets with
that situation than before.
|
|
|
|
| |
Ref https://github.com/rails/rails/commit/59ff1ba30d9f4d34b4d478104cc3f453e553c67a#diff-38fb97fba84b1ef0f311c4110a597c44R35
|
|\
| |
| |
| | |
Ensure that `delete_all` on collection proxy returns affected count
|
| |
| |
| |
| |
| |
| |
| | |
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 delete on collection association to clear stale
offsets of removed records.
|
|
|
|
| |
Follow up ba4e68f577efc76f351d30a2914e29942b97830e.
|
| |
|
|
|
|
| |
https://travis-ci.org/rails/rails/jobs/459534536#L1280
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
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.
|
|
|
|
|
|
|
|
|
| |
The PR#31819 changed `#preloaders_on` and added some test,
then #33938 reverted changes that were added to the method in #31819.
Since changes in the method were reverted and as mentioned in the
comment https://github.com/rails/rails/pull/31819#discussion_r221847481
that titles of the tests added in #31819 don't reflect implementation I
think we can remove those test for now.
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
Since #33844, eager loading/preloading with too many and/or too large
ids won't be broken by pre-checking whether the value is constructable
or not.
But the pre-checking caused the type to be evaluated at relation build
time instead of at the query execution time, that is breaking an
expectation for some apps.
I've made the pre-cheking lazy as much as possible, that is no longer
happend at relation build time.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
Starting in Rails 5.0.0 and still present in Rails 5.2.1, `singular_ids`
got memoized and didn't reload after more items were added to the
relation.
Although 19c8071 happens to fix the issue, it only adds tests for
`has_many` relations while this bug only affected
`has_and_belongs_to_many` relations.
This commit adds a regression test to ensure it never happens again with
`habtm` relations.
Ensures #34179 never gets reproduced.
|
|
|
|
|
|
|
|
|
| |
`developers.name desc` was added at d59f3a7, but any test case isn't
failed even if the `developers.name desc` is removed since all tested
developers are consistently ordered on both `name` and `id`.
I changed one developers creation ordering to ensure to test that
`project.developers` is ordered by `developers.name desc`.
|
|\
| |
| | |
Generate delegation methods to named scope in the definition time
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| | |
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.
|
|\ \
| |/
|/|
| |
| | |
christophemaximin/fix-activerecord-clearing-of-query-cache
Fix inconsistent behavior by clearing QueryCache when reloading associations
|
| | |
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| | |
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`.
|
| | |
|
|/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
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.
|
|
|
|
| |
Follow up 811be477786455d144819a5e9fbb7f9f54b8da69.
|
|
|
|
|
|
|
|
|
| |
`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.
|
|
|
|
| |
cherry-picked from https://github.com/rails/rails/pull/33763
|
|
|
|
| |
missing records
|
|
|
|
| |
Since counter cache handles touch option too.
|