aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/reflection.rb
Commit message (Collapse)AuthorAgeFilesLines
* `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`.
* / Remove duplicated `table_name`Ryuta Kamizono2017-08-121-8/+0
|/ | | | It can use `AbstractReflection#table_name` simply.
* Changed join_fk private method to join_foreign_key public methodchopraanmol12017-08-091-9/+9
|
* Use `Concurrent::Map` than `Mutex` and `Mutex_m` for statement cachesRyuta Kamizono2017-08-041-6/+3
| | | | | Statement caches are used as a concurrent map. It will more clarify to using `Concurrent::Map`.
* Passing `klass` to `StatementCache.new`Ryuta Kamizono2017-08-041-2/+2
| | | | | | Actually `StatementCache#execute` is always passed the same klass that the owner klass of the connection when the statement cache is created. So passing `klass` to `StatementCache.new` will make more DRY.
* Use frozen-string-literal in ActiveRecordKir Shatrov2017-07-191-0/+2
|
* Fix unscoping `default_scope` in STI associationsRyuta Kamizono2017-07-191-2/+4
| | | | | | | Since 5c71000, it has lost to be able to unscope `default_scope` in STI associations. This change will use `.empty_scope?` instead of `.values.empty?` to regard as an empty scope if only have `type_condition`.
* Post.joins(:users) should not be affected by `User.current_scope`Sean Griffin2017-07-171-4/+2
| | | | | | | | | | | | | | | | | | | | | | | | This change was introduced by #18109. The intent of that change was to specifically apply `unscoped`, not to allow all changes to `current_scope` to affect the join. The idea of allowing `current_scope` to affect joins is interesting and potentially more consistent, but has sever problems associated with it. The fact that we're specifically stripping out joins indicates one such problem (and potentially leads to invalid queries). Ultimately it's difficult to reason about what `Posts.joins(:users)` actually means if it's affected by `User.current_scope`, and it's difficult to specifically control what does or doesn't get added. If we were starting from scratch, I don't think I'd have `joins` be affected by `default_scope` either, but that's too big of a breaking change to make at this point. With this change, we no longer apply `current_scope` when bringing in joins, with the singular exception of the motivating use case which introduced this bug, which is providing a way to *opt-out* of having the default scope apply to joins. Fixes #29338.
* Fix eager loading association with scope including joinsRyuta Kamizono2017-07-041-7/+11
| | | | Fixes #28324.
* 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.
* Merge pull request #29540 from kirs/rubocop-frozen-stringMatthew Draper2017-07-021-0/+1
|\ | | | | | | Enforce frozen string in Rubocop
| * Enforce frozen string in RubocopKir Shatrov2017-07-011-0/+1
| |
* | Merge pull request #28808 from fschwahn/fix-polymorphic-automic-inverseMatthew Draper2017-07-011-4/+3
|\ \ | |/ |/| Fix automatic inverse for polymorphic interfaces
| * Remove :polymorphic from INVALID_AUTOMATIC_INVERSE_OPTIONSFabian Schwahn2017-04-201-4/+3
| | | | | | | | | | | | | | This makes automatic inverse detection possible for polymorphic :has_one & :has_many possible. This resolves a number of issues, eg. `touch: true` on polymorphic relationships (#16446) and automatically setting inverse associations on newly built objects (#15028, #21843).
* | Merge pull request #28928 from kamipo/remove_habtm_initializeRafael França2017-06-281-4/+0
|\ \ | | | | | | Remove `HasAndBelongsToManyReflection#initialize`
| * | Remove `HasAndBelongsToManyReflection#initialize`Ryuta Kamizono2017-04-291-4/+0
| |/ | | | | | | It is delegating `super` only.
* | Fix eager loading to respect `store_full_sti_class` settingRyuta Kamizono2017-06-291-1/+1
| |
* | Move building constraint to `join_scope` in `Reflection`Ryuta Kamizono2017-06-271-2/+11
| |
* | Move constructing polymorphic type to `join_scope` in `Reflection`Ryuta Kamizono2017-06-271-4/+7
| |
* | Move constructing join scope to `Reflection`Ryuta Kamizono2017-06-261-4/+11
| |
* | Extract `build_scope` and `predicate_builder` in `Reflection`Ryuta Kamizono2017-06-241-7/+10
| |
* | Don't expose methods and attrs for internal usageRyuta Kamizono2017-05-301-17/+19
| |
* | Merge pull request #29098 from kamipo/fix_association_with_extension_issuesMatthew Draper2017-05-301-0/+4
|\ \ | | | | | | | | | Fix association with extension issues
| * | Fix association with extension issuesRyuta Kamizono2017-05-281-0/+4
| | | | | | | | | | | | | | | | | | | | | | | | This fixes the following issues. * `association_scope` doesn't include `default_scope`. Should use `scope` instead. * We can't use `method_missing` for customizing existing method. * We can't use `relation_delegate_class` for sharing extensions. Should extend per association.
* | | Add option for class_attribute default (#29270)David Heinemeier Hansson2017-05-291-4/+2
|/ / | | | | | | | | | | | | | | | | | | | | | | * Allow a default value to be declared for class_attribute * Convert to using class_attribute default rather than explicit setter * Removed instance_accessor option by mistake * False is a valid default value * Documentation
* | Fix crashing on circular left join references with scopingRyuta Kamizono2017-05-241-1/+1
| | | | | | | | Follow up of #25702.