aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations/preloader
Commit message (Collapse)AuthorAgeFilesLines
* Don't unset foreign key when preloading missing recordEugene Kenny2018-03-241-2/+2
| | | | | | | | | When a belongs to association's target is set, its foreign key is now updated to match the new target. This is the correct behaviour when a new record is assigned, but not when the existing record is preloaded. As long as we mark the association as loaded, we can skip setting the target when the record is missing and avoid clobbering the foreign key.
* Extract all `base_class.name` as `polymorphic_name`Ryuta Kamizono2018-03-041-1/+1
| | | | | | | This is an alternative of #29722, and follow up of #32048. This does not change the current behavior, but makes it easier to modify all polymorphic names consistently.
* Association creation and finding should work consistently (#32048)Ryuta Kamizono2018-02-261-1/+1
| | | | | | | | | | | This is an alternative of #29722, and revert of #29601 and a1fcbd9. Currently, association creation and normal association finding doesn't respect `store_full_sti_class`. But eager loading and preloading respect the setting. This means that if set `store_full_sti_class = false` (`true` by default), eager loading and preloading can not find created polymorphic records. Association creation and finding should work consistently.
* Use private attr_readerRyuta Kamizono2018-02-231-2/+1
| | | | | Since #32028, Rails 6 requires Ruby 2.3+. No longer needed workaround for Ruby 2.2 "private attribute?" warning.
* Avoid extra calls to to_sDaniel Colson2018-01-291-2/+2
| | | | | | | With #31615 `type_for_attribute` accepts either a symbol as well as a string. `has_attribute?` and `attribute_alias` also accept either. Since these methods call `to_s` on the argument, we no longer need to do that at the call site.
* Remove useless preloader classesRyuta Kamizono2017-11-109-85/+8
| | | | | | | | | They are only different by one line of code which doesn't deserve a hierarchy of 7 classes. Closes #31079. [Ryuta Kamizono & Bogdan Gusiev]
* Don't expose accessors which are internal used onlyRyuta Kamizono2017-11-081-1/+3
|
* Don't expose internal methods in `Preloader::ThroughAssociation`Ryuta Kamizono2017-11-081-8/+7
| | | | `through_reflection` and `source_reflection` are used only in the class.
* Remove useless `associated_records_by_owner`Ryuta Kamizono2017-11-072-16/+10
| | | | | | | | `associated_records_by_owner` had returned customizing result before calling `associate_records_to_owner` for through association subclasses. Since #22115, `associate_records_to_owner` is called in the method and not returned owner and result pairs. Removing the method will reduce method call and block call nesting.
* Refactor Preloader CodeBogdan Gusiev2017-11-063-81/+40
|
* Fix preloading polymorphic multi-level through associationRyuta Kamizono2017-11-061-1/+7
| | | | | | | | | | | This is partially fixed by e617fb57 when through association has already loaded. Otherwise, second level through association should respect `preload_scope`. Fixes #30242. Closes #30076. [Ryuta Kamizono & CicholGricenchos]
* Fix preloading polymorphic association when through association has already ↵Ryuta Kamizono2017-11-061-4/+16
| | | | | | | | | | loaded If through association has already loaded, `source_type` is ignored to loaded through records. The loaded records should be filtered by `source_type` in that case. Fixes #30904.
* PERF: Partially recover some performance when preloading.Guo Xiang Tan2017-09-261-9/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Benchmark Script: ``` require 'active_record' require 'benchmark/ips' ActiveRecord::Base.establish_connection(ENV.fetch('DATABASE_URL')) ActiveRecord::Migration.verbose = false ActiveRecord::Schema.define do create_table :users, force: true do |t| t.string :name, :email t.integer :topic_id t.timestamps null: false end create_table :topics, force: true do |t| t.string :title t.timestamps null: false end end attributes = { name: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.', email: 'foobar@email.com' } class Topic < ActiveRecord::Base has_many :users end class User < ActiveRecord::Base belongs_to :topic end 100.times do User.create!(attributes) end users = User.first(50) Topic.create!(title: 'This is a topic', users: users) Benchmark.ips do |x| x.config(time: 10, warmup: 5) x.report("preload") do User.includes(:topic).all.to_a end end ``` Before: ``` Calculating ------------------------------------- preload 40.000 i/100ms ------------------------------------------------- preload 407.962 (± 1.5%) i/s - 4.080k ``` After: ``` alculating ------------------------------------- preload 43.000 i/100ms ------------------------------------------------- preload 427.567 (± 1.6%) i/s - 4.300k ```
* Remove unused delegation to `reflection.options` in `Preloader::Association`Ryuta Kamizono2017-09-182-4/+1
|
* The name of the key on the associated record is abstracted as ↵Ryuta Kamizono2017-09-184-14/+5
| | | | `reflection.join_primary_key`
* The name of the key on the owner is abstracted as `reflection.join_foreign_key`Ryuta Kamizono2017-09-184-17/+5
|
* Extract `associate_records_to_owner` to refactor `Preloader::Association`Ryuta Kamizono2017-09-183-20/+14
| | | | | | | | Since we have `Preloader#preload`, `Preloader::Association#preload` is a little confusing. And also, since the `preload` method is an abstract method, it is hard to read where `associated_records_by_owner` is called. This refactors `Preloader::Association` to ease to read where `associated_records_by_owner` is called.
* Don't pass `reflection_scope` to `preload_scope` if `reflection.scope` isn't ↵Ryuta Kamizono2017-09-181-1/+3
| | | | | | | | | given Related 2b5f5cdd7c1d95716de6a206b6d09ccbb006dc17. If `reflection.scope` isn't given, `reflection_scope` is always empty scope. It is unnecessary to merge it.
* Return `through_scope` only if the scope is not empty scopeRyuta Kamizono2017-09-181-4/+2
| | | | | | | | | Related 2b5f5cdd7c1d95716de6a206b6d09ccbb006dc17. If `through_scope` is empty scope, it is unnecessary to merge it. And also, comparing relations is a little expensive (will cause `build_arel`). It is enough to use `empty_scope?` to determine whether empty scope.
* Remove useless condition in `reset_association`Ryuta Kamizono2017-09-181-2/+1
| | | | `through_scope` is not empty scope if `options[:source_type]` is given.
* PERF: Incorrect memoization in ↵Guo Xiang Tan2017-09-111-1/+5
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | `ActiveRecord::Associations::Preloader::Association`. ``` require 'active_record' require 'benchmark/ips' ActiveRecord::Base.establish_connection(ENV.fetch('DATABASE_URL')) ActiveRecord::Migration.verbose = false ActiveRecord::Schema.define do create_table :users, force: true do |t| t.string :name, :email t.integer :topic_id t.timestamps null: false end create_table :topics, force: true do |t| t.string :title t.timestamps null: false end end attributes = { name: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.', email: 'foobar@email.com' } class Topic < ActiveRecord::Base has_many :users end class User < ActiveRecord::Base belongs_to :topic end 100.times do User.create!(attributes) end users = User.first(50) 100.times do Topic.create!(title: 'This is a topic', users: users) end Benchmark.ips do |x| x.config(time: 10, warmup: 5) x.report("preload") do User.includes(:topic).all.to_a end end ``` ``` Calculating ------------------------------------- preload 25.000 i/100ms ------------------------------------------------- preload 251.772 (± 1.2%) i/s - 2.525k ``` ``` Calculating ------------------------------------- preload 26.000 i/100ms ------------------------------------------------- preload 270.392 (± 1.1%) i/s - 2.704k ```
* Assigning `values` is only necessary when `reflection_scope.where_clause` is ↵Ryuta Kamizono2017-09-051-1/+1
| | | | | | not empty Because `reflection_scope.values` will create extra new hash.
* Don't merge `reflection_scope` if `reflection.scope` isn't givenRyuta Kamizono2017-09-051-1/+1
| | | | | If `reflection.scope` isn't given, `reflection_scope` is always `klass.unscoped`. it is unnecessary to merge it.
* Scope in associations should treat nil as `all`Ryuta Kamizono2017-09-041-1/+1
| | | | | | | | Defined scope treats nil as `all`, but scope in associations isn't so. If the result of the scope is nil, most features on associations will be broken. It should treat nil as `all` like defined scope. Fixes #20823.
* Fix preloading through association with custom scopeRyuta Kamizono2017-09-021-1/+6
| | | | | | | | | | If `reflection_scope.where_clause` is not empty, `through_scope` should be joined the source association. But if `values[:references]` in `reflection_scope` is empty, the source association will not be joined. It should use `source_reflection.table_name` in that case. Fixes #22535. Closes #28763.
* `values[:includes]` in `reflection_scope` is not compatible with `through_scope`Ryuta Kamizono2017-09-021-1/+6
| | | | | | | | | | | | | | | | | Without this fix, preloading `:comments_with_include` will cause the following error: ``` % bundle exec ruby -w -Itest test/cases/associations/eager_test.rb -n test_eager_with_has_many_through_join_model_with_include Using sqlite3 Run options: -n test_eager_with_has_many_through_join_model_with_include --seed 1502 E Error: EagerAssociationTest#test_eager_with_has_many_through_join_model_with_include: ActiveRecord::AssociationNotFoundError: Association named 'post' was not found on Post; perhaps you misspelled it? ```
* Don't call `scope.eager_loading?` when `reflection_scope.where_clause` is emptyRyuta Kamizono2017-09-021-10/+10
| | | | | If `reflection_scope.where_clause` is empty, `scope` isn't changed. So `scope.eager_loading?` is always false.
* Through scope should not be affected by scopingRyuta Kamizono2017-08-151-11/+1
| | | | | | Follow up of #29834. Fixes #30266.
* Remove single element array preprocessRyuta Kamizono2017-07-261-1/+1
| | | | | Since 213796f, array predicate handler supports making binds, so the preprocess is no longer needed.
* Use frozen-string-literal in ActiveRecordKir Shatrov2017-07-199-0/+18
|
* Merge pull request #29033 from kamipo/make_preload_query_to_prepared_statementsSean Griffin2017-07-181-1/+1
|\ | | | | Make preload query to preparable
| * Make preload query to preparableRyuta Kamizono2017-07-071-1/+1
| | | | | | | | | | | | | | Currently preload query cannot be prepared statements even if `prepared_statements: true` due to array handler in predicate builder doesn't support making bind params. This makes preload query to preparable by don't passing array value if possible.
* | Fix unscoping `default_scope` for `Preloader`Ryuta Kamizono2017-07-191-1/+11
|/
* Fix preloading association with scope including joinsRyuta Kamizono2017-07-042-46/+25
|
* Remove unused `association_key` and `table` methods in `Preloader::Association`Ryuta Kamizono2017-07-041-10/+0
| | | | These are no longer used since b98668decb9712f26118de57623fd15d7d28646d.
* Revert "Merge pull request #29540 from kirs/rubocop-frozen-string"Matthew Draper2017-07-029-9/+0
| | | | | This reverts commit 3420a14590c0e6915d8b6c242887f74adb4120f9, reversing changes made to afb66a5a598ce4ac74ad84b125a5abf046dcf5aa.
* Enforce frozen string in RubocopKir Shatrov2017-07-019-0/+9
|
* Merge pull request #29129 from kamipo/prevent_extra_through_scopeRafael França2017-06-281-2/+4
|\ | | | | Prevent extra `through_scope`
| * Prevent extra `through_scope`Ryuta Kamizono2017-05-181-2/+4
| | | | | | | | We can reuse `through_scope` for `reset_association`.
* | Don't expose methods and attrs for internal usageRyuta Kamizono2017-05-303-6/+5
|/
* Remove useless `target_records_from_association`Ryuta Kamizono2017-05-041-11/+3
| | | | Since through association is always loaded by `preloader.preload`.
* Add more rubocop rules about whitespacesRafael Mendonça França2016-10-292-4/+4
|
* Merge pull request #26379 from kamipo/remove_unnecessary_query_scopeAndrew White2016-09-051-4/+0
|\ | | | | Remove unnecessary `query_scope`
| * Remove unnecessary `query_scope`Ryuta Kamizono2016-08-161-4/+0
| |
* | Ensure that inverse associations are set before running callbacksSean Griffin2016-08-313-5/+17
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | If a parent association was accessed in an `after_find` or `after_initialize` callback, it would always end up loading the association, and then immediately overwriting the association we just loaded. If this occurred in a way that the parent's `current_scope` was set to eager load the child, this would result in an infinite loop and eventually overflow the stack. For records that are created with `.new`, we have a mechanism to perform an action before the callbacks are run. I've introduced the same code path for records created with `instantiate`, and updated all code which sets inverse instances on newly loaded associations to use this block instead. Fixes #26320.
* | Merge pull request #24099 from k0kubun/preserve-readonlyRafael Mendonça França2016-08-181-1/+1
|\ \ | |/ |/| | | Preserve readonly flag only for readonly association
| * Preserve readonly flag only for readonly associationTakashi Kokubun2016-07-301-1/+1
| | | | | | | | Fixes #24093
* | Add `Style/EmptyLines` in `.rubocop.yml` and remove extra empty linesRyuta Kamizono2016-08-071-1/+0
| |
* | applies remaining conventions across the projectXavier Noria2016-08-063-6/+0
| |
* | normalizes indentation and whitespace across the projectXavier Noria2016-08-064-117/+117
|/