aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/reflection.rb
Commit message (Collapse)AuthorAgeFilesLines
* Enable `Layout/EmptyLinesAroundAccessModifier` copRyuta Kamizono2019-06-131-2/+0
| | | | | | | | | | | We sometimes say "✂️ newline after `private`" in a code review (e.g. https://github.com/rails/rails/pull/18546#discussion_r23188776, https://github.com/rails/rails/pull/34832#discussion_r244847195). Now `Layout/EmptyLinesAroundAccessModifier` cop have new enforced style `EnforcedStyle: only_before` (https://github.com/rubocop-hq/rubocop/pull/7059). That cop and enforced style will reduce the our code review cost.
* Make scope arity check consistent (#36134)Rob Trame2019-05-011-1/+1
| | | | | | | | * Make scope arity check consistent * Add test for arity change [Rob Trame + Rafael Mendonça França]
* Deduplicate Active Record reflection namesJean Boussier2019-04-101-2/+2
|
* Fix eager loading polymorphic association with mixed table conditionsRyuta Kamizono2019-02-181-14/+10
| | | | | | 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.
* Revert "Merge pull request #33729 from kddeisz/plural-automatic-inverse"Ryuta Kamizono2019-02-011-15/+5
| | | | | | | This reverts commit ed1eda271c7ac82ecb7bd94b6fa1b0093e648a3e, reversing changes made to 3d2caab7dc92a13d4dd369678d5b4ce659df8e52. Reason: 7c3da6e0030aa080fcb89af58b094ed50d861a44
* Find inverse associations with plural namesKevin Deisz2018-08-271-5/+15
| | | | | | | | | | | | | | | | Previously ActiveRecord couldn't find inverse associations if they were plural, which is a pretty standard use case. This commit changes the behavior to first attempt to find the singular version, then attempt to find the plural version. That makes it work and find plural associations as in the example below: ``` class Post has_many :comments end class Comment belongs_to :post end ``` Previously the `:post` association reflection would only attempt to find a `comment` inverse, as opposed to both a `comment` and `comments` inverse.
* Ensure reflection_class_for is privateKevin Deisz2018-05-311-24/+27
|
* Move ActiveStorage reflection logic entirely into ActiveStorageKevin Deisz2018-05-311-44/+2
|
* Reflection for attachmentsKevin Deisz2018-05-301-17/+60
| | | | Add the ability to reflect on the attachments that have been defined using ActiveRecord::Reflection.
* Exception wording changeCory Becker2018-05-211-1/+1
|
* 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.
* Partly revert 91b30a001b79096b60d9424a4664a417dce0b767Ryuta Kamizono2018-02-271-1/+7
| | | | | | | | | | | Actually `reflection.klass` should be valid AR model unless `polymorphic?`. Previously it worked limitedly by ignoring `NameError` even if `reflection.klass` is invalid, and our isolated testing depends on the limited working. Probably we should also check the klass validity in `check_validity!` properly. Until that time, I restored the error suppression for now. Closes #32113.
* Merge pull request #31895 from ↵Ryuta Kamizono2018-02-261-10/+7
|\ | | | | | | | | kamipo/do_not_attempt_to_find_inverse_of_polymorphic Make `reflection.klass` raise if `polymorphic?` not to be misused
| * Do not attempt to automatically find the inverse of a polymorphic ↵Ryuta Kamizono2018-02-191-10/+4
| | | | | | | | | | | | | | | | | | `belongs_to` association We can't automatically find the inverse of a polymorphic `belongs_to` association without context. [Ryuta Kamizono & Eric K Idema]
| * Make `reflection.klass` raise if `polymorphic?` not to be misusedRyuta Kamizono2018-02-191-1/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This is an alternative of #31877 to fix #31876 caused by #28808. This issue was caused by a combination of several loose implementation. * finding automatic inverse association of polymorphic without context (caused by #28808) * returning `klass` even if `polymorphic?` (exists before #28808) * loose verification by `valid_inverse_reflection?` (exists before #28808) This makes `klass` raise if `polymorphic?` not to be misused. This issue will not happen unless polymorphic `klass` is misused. Fixes #31876. Closes #31877.
* | 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.
* Rails 6 requires Ruby 2.3+Jeremy Daer2018-02-171-4/+2
|
* Avoid passing unnecessary arguments to relationDaniel Colson2018-01-241-1/+5
| | | | | | | | | | | | Most of the time the table and predicate_builder passed to Relation.new are exactly the arel_table and predicate builder of the given klass. This uses klass.arel_table and klass.predicate_builder as the defaults, so we don't have to pass them in most cases. This does change the signaure of both Relation and AssocationRelation. Are we ok with that?
* Remove the deprecated `:conditions` option in ↵Ryuta Kamizono2018-01-111-1/+1
| | | | | | `INVALID_AUTOMATIC_INVERSE_OPTIONS` This option was moved into active_record_deprecated_finders in ac4d101.
* Fix `stale_state` for nested `has_many :through` associationsRyuta Kamizono2018-01-101-8/+0
| | | | Need reloading when through record has replaced.
* Merge pull request #16314 from ↵Ryuta Kamizono2018-01-101-0/+8
|\ | | | | | | | | | | 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/+4
| | | | | | | | Fixes: #16313
* | Fix newly added reflection order when redefining associationRyuta Kamizono2018-01-041-1/+2
| | | | | | | | | | | | | | | | | | | | Currently reflections keeps the order when first added even if when redefining association. As a result of the order, redefining through association which use newly added association will raise `HasManyThroughOrderError`. We need to redefine reflection order as well when redefining association. Fixes #31068.
* | Remove `association_primary_key_type` from `AssociationReflection` and ↵Ryuta Kamizono2018-01-011-8/+0
| | | | | | | | | | | | | | | | `ThroughReflection` This method was introduced in #26718, which is internally used only in `CollectionAssociation`. There is no need to be in the reflection classes.
* | Refactor delegating `join_primary_key` instead of `join_keys` and ↵Ryuta Kamizono2018-01-011-2/+2
| | | | | | | | | | | | | | `association_primary_key` in `TableMetadata` Because `join_primary_key` is called by `join_keys` and it is to abstract calling `association_primary_key`.
* | `source_type_scope` should respect correct table aliasRyuta Kamizono2017-10-301-7/+3
| | | | | | | | | | | | | | | | | | `join_scopes` in `PolymorphicReflection` is passed aliased `table`, so it should be respected for `source_type_scope`. Closes #13969. Fixes #13920. Fixes #15190.
* | `PolymorphicReflection#scopes` is no longer used since a5651eb5Ryuta Kamizono2017-10-301-5/+0
| |
* | delegate scope forpavel2017-10-271-1/+1
| |
* | Remove deprecated methd `#scope_chain`Rafael Mendonça França2017-10-231-6/+0
| |
* | Remove deprecated support to passing a class to `:class_name` on associationsRafael Mendonça França2017-10-231-8/+1
| |
* | Don't generate `foreign_type` if `options[:polymorphic]` is not givenRyuta Kamizono2017-09-271-1/+1
| | | | | | | | | | Because the reflection doesn't have `foreign_type` unless the association is a polymorphic association.
* | PERF: Restore memoization when preloading associations.Guo Xiang Tan2017-09-251-6/+5
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Benchmark Script ``` require 'active_record' require 'benchmark/ips' require 'ruby-prof' require 'memory_profiler' require 'byebug' 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 26.000 i/100ms ------------------------------------------------- preload 265.347 (± 3.0%) i/s - 2.652k ``` After ``` Calculating ------------------------------------- preload 39.000 i/100ms ------------------------------------------------- preload 406.053 (± 1.7%) i/s - 4.095k ```
* | The name of the key on the associated record is abstracted as ↵Ryuta Kamizono2017-09-181-9/+9
| | | | | | | | `reflection.join_primary_key`
* | Remove duplicated `klass` method in `AssociationReflection`Ryuta Kamizono2017-09-081-16/+11
| | | | | | | | | | The superclass (`MacroReflection`) already have the same method definition.
* | Remove unused `primary_key_type` and `quoted_table_name` in `Reflection`Ryuta Kamizono2017-09-081-8/+0
| | | | | | | | | | `primary_key_type` is no longer used since #26718. `quoted_table_name` is no longer used since Rails 3.1.
* | Don't pass `table` to `last_chain_scope` and `next_chain_scope`Ryuta Kamizono2017-09-071-2/+2
| | | | | | | | | | | | Because `table` is part of `reflection`, don't need to pass it explicitly. And also, naming `alias_name` to `table` is a little confusing. `aliased_table` is preferable than `alias_name`.
* | `RuntimeReflection` is not a subclass of `PolymorphicReflection`Ryuta Kamizono2017-09-071-26/+6
| | | | | | | | | | | | `PolymorphicReflection` is an internal class that is used in `ThroughReflection`. But `RuntimeReflection` is used for the head of chain in `AssociationScope`. These are totally different things.
* | `has_many :through` with unscope should affect to through scopeRyuta Kamizono2017-09-071-2/+0
| | | | | | | | | | | | | | | | | | 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.
* | Scope in associations should treat nil as `all`Ryuta Kamizono2017-09-041-3/+3
| | | | | | | | | | | | | | | | 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.
* | `@previous_reflection.options[:source_type]` in `PolymorphicReflection` is ↵Ryuta Kamizono2017-08-281-10/+2
| | | | | | | | | | | | | | always true Because `add_as_polymorphic_through` is only called when `options[:source_type]` is true.
* | Remove unused `RuntimeReflection#alias_candidate`Ryuta Kamizono2017-08-281-4/+0
| | | | | | | | `RuntimeReflection#alias_candidate` is no longer used since 0408e212.
* | Automatically guess the inverse associations for STIyui-knk2017-08-221-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | ActiveRecord associations automatically guess the inverse associations. But this feature does not work correctly on assoctions for STI. For example, before this commit ``` class Post < ActiveRecord::Base belongs_to :author end class SpecialPost < Post; end class Author < ActiveRecord::Base has_many :posts has_many :special_posts end ``` `author.posts.first.author` works correctly, but `author.special_posts.first.author` does not work correctly.
* | Through scope should not be affected by scopingRyuta Kamizono2017-08-151-7/+2
| | | | | | | | | | | | Follow up of #29834. Fixes #30266.
* | Merge pull request #30208 from kamipo/extract_primary_key_to_abstract_reflectionRafael França2017-08-141-8/+4
|\ \ | | | | | | Extract `primary_key` to `AbstractReflection`
| * | Extract `primary_key` to `AbstractReflection`Ryuta Kamizono2017-08-121-8/+4
| | |
* | | Merge pull request #27609 from kamipo/fix_association_primary_keyRafael França2017-08-141-0/+4
|\ \ \ | | | | | | | | Fix `reflection.association_primary_key` for `has_many` association
| * | | Fix `reflection.association_primary_key` for `has_many` associationsRyuta Kamizono2017-08-131-0/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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 #30231 from kamipo/remove_unused_source_type_infoRafael França2017-08-141-10/+7
|\ \ \ \ | | | | | | | | | | Remove unused `source_type_info` in `RuntimeReflection`
| * | | | Remove unused `source_type_info` in `RuntimeReflection`Ryuta Kamizono2017-08-131-10/+7
| |/ / / | | | | | | | | | | | | | | | | `source_type_info` is only used for `constraints` in `PolymorphicReflection`.
* / / / Remove duplicated `join_id_for`Ryuta Kamizono2017-08-131-10/+2
|/ / / | | | | | | | | | The primary key on the owner record is abstracted as `join_foreign_key`.