aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations/join_dependency.rb
Commit message (Collapse)AuthorAgeFilesLines
* Fix merging left_joins to maintain its own `join_type` contextRyuta Kamizono2019-04-271-9/+10
| | | | | | | | | | | | | | | This fixes a regression for #35864. Usually, stashed joins (mainly eager loading) are performed as LEFT JOINs. But the case of merging joins/left_joins of different class, that (stashed) joins are performed as the same `join_type` as the parent context for now. Since #35864, both (joins/left_joins) stashed joins might be contained in `joins_values`, so each stashed joins should maintain its own `join_type` context. Fixes #36103.
* Avoid unneeded expanded column aliases array cachingRyuta Kamizono2018-07-101-6/+3
|
* Don't extract `readonly_value` each timeRyuta Kamizono2018-07-031-5/+1
|
* Don't share seen object cache between different join nodes in eager loadingRyuta Kamizono2018-07-031-2/+2
| | | | | | | | | | | Currently, the seen object cache is shared if join nodes have the same target class. But it is a wrong assumption, we can't share the seen object cache between different join nodes (e.g. `:readonly_account` and `:accounts` have the same target class `Account`, but the instances have the different state `readonly`). Fixes #26805. Closes #27737.
* Use `reflection.alias_candidate` in `table_alias_for`Ryuta Kamizono2018-06-261-1/+1
| | | | | | | | | | | | | | | The "join" affix in `table_alias_for` was added 12 years ago at 02d3444 to address poor alias tracking. `AssociationScope` no longer uses the "join" suffixed alias since 0408e21 and had been removed at a1ec8b5. `table_alias_for` is the last place that using the useless legacy suffixed alias, but we can't remove the suffix since some test cases directly refers the alias name by `where` with string literal, so at least removing the suffix would break our test cases. (e.g. https://github.com/rails/rails/blob/b2eb1d1c55a59fee1e6c4cba7030d8ceb524267c/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb#L699-L731).
* Remove all `aliases` passing from `JoinDependency`Ryuta Kamizono2018-06-201-2/+2
| | | | Follow up of 15367a2c674bf19eeefa12ccb64391bdd50d883d.
* Remove useless `aliases` argument from `JoinDependency#construct`Ryuta Kamizono2018-06-201-5/+5
| | | | | Since `aliases` is a part of `JoinDependency` and already cached at 1a723c65bbe91ad969b67416233d20eff6d2a46a.
* Remove unused `rs` argument from `JoinDependency#construct`Ryuta Kamizono2018-06-201-5/+5
| | | | It was no longer used since d7ddaa530fd1b94e22d745cbaf2e8a5a34ee9734.
* Ensure to calculate column aliases after all table aliases are constructedRyuta Kamizono2018-06-191-40/+38
| | | | | | | | | | | | | | | | | Currently, column aliases which is used for eager loading are calculated before constructing all table aliases in FROM clause. `JoinDependency#join_constraints` constructs table aliases for `joins` first, and then always re-constructs table aliases for eager loading. If both `joins` and eager loading are given a same table association, the re-construction would cause the discrepancy between column aliases and table aliases. To avoid the discrepancy, the column aliases should be calculated after all table aliases are constructed. Fixes #30603.
* Child joins should be aliased when merging relationsChalo Fernandez2018-06-061-1/+1
| | | | | | | | | | | | | | | | | | | | | | Rails 5.2 does not alias child joins, causing an error about duplicated table/fields: Example: Using some code like: `Post.joins(:author, :categorizations).merge(Author.select(:id)).merge(Categorization.joins(:author))` *Before this fix:* ` SELECT ... FROM "posts" INNER JOIN "authors" ON ... INNER JOIN "authors" ON ... ` *After this fix:* ` SELECT ... FROM "posts" INNER JOIN "authors" ON ... INNER JOIN "authors" "authors_categorizations" ON ... ` Before 5.2, Rails aliased the joins, but wrongfully transformed them into a LEFT OUTER JOIN. This fix will keep them as INNER JOINS, but make sure child joins are aliased, to avoid errors.
* Eager loading with polymorphic associations should behave consistentlyRyuta Kamizono2018-03-041-4/+2
| | | | | | | | | | This reverts ignoring polymorphic error introduced at 02da8ae. What the ignoring want to solve was caused by force eager loading regardless of whether it is necessary, but it has been fixed by #29043. The ignoring is now only causing a mismatch of `exists?` behavior with `to_a`, `count`, etc. It should behave consistently.
* Remove staled comment for `JoinDependency#initialize`Ryuta Kamizono2018-03-021-21/+0
| | | | | | | | | | | This comment was added at 070dda2. That arguments has already been changed since those are internal nodoc classes, but the comment does not reflect the current state. I decided to remove the staled comment since it is not useful for understanding what the class does. [ci skip]
* Joined tables in association scope doesn't use the same aliases with the ↵Ryuta Kamizono2017-10-091-3/+4
| | | | | | | | | parent relation's aliases Building association scope in join dependency should respect the parent relation's aliases to avoid using the same alias name more than once. Fixes #30681.
* Decouple building `AliasTracker` from `JoinDependency`Ryuta Kamizono2017-10-081-2/+2
| | | | | This is preparation to respect parent relation's alias tracking for fixing #30681.
* Ensure `AliasTracker` respects a custom table nameRyuta Kamizono2017-09-301-1/+1
|
* Scope in associations should treat nil as `all`Ryuta Kamizono2017-09-041-1/+2
| | | | | | | | 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.
* Should work inverse association when eager loadingRyuta Kamizono2017-08-251-3/+3
| | | | | | | This regression was caused by caa178c1. The block for `set_inverse_instance` should also be passed to join dependency. Fixes #30402.
* Remove useless `JoinInformation`Ryuta Kamizono2017-07-251-2/+2
| | | | | Since 213796f removed `binds`, `JoinInformation` only contain `joins`. So it is enough to return `joins` simply.
* Use frozen-string-literal in ActiveRecordKir Shatrov2017-07-191-0/+2
|
* Fix `JoinDependency` with using a custom tableRyuta Kamizono2017-07-181-2/+2
| | | | | | | | | | | | | | | Without this fix, `JoinDependency` doesn't use a custom table alias: ``` % ARCONN=sqlite3 be ruby -w -Itest test/cases/relations_test.rb -n test_using_a_custom_table_with_joins_affects_the_wheres Using sqlite3 Run options: -n test_using_a_custom_table_with_joins_affects_the_wheres --seed 14531 E Error:RelationTest#test_using_a_custom_table_with_joins_affects_the_wheres: ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: posts.author_id: SELECT "omg_posts".* FROM "posts" "omg_posts" INNER JOIN "authors" ON "authors"."id" = "posts"."author_id" WHERE "omg_posts"."title" = ? LIMIT ? ```
* Remove useless `aliased_table_name` in `JoinDependency`Ryuta Kamizono2017-07-151-5/+1
| | | | If `table.table_alias` is not nil, it is enough to use `table` simply.
* 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 #29506 from pat/frozen-string-literalsMatthew Draper2017-07-021-2/+1
|\ \ | |/ |/| | | Make ActiveSupport frozen-string-literal friendly.
| * Make ActiveRecord frozen string literal friendly.Pat Allan2017-06-201-2/+1
| |
* | The AliasTracker#aliased_table_for needs the type caster for the joined ↵Ray Zane2017-06-251-2/+3
| | | | | | | | association, not the join root
* | Keep INNER JOIN when merging relationsMaxime Lapointe2017-06-201-3/+3
|/ | | | | | | | | | | | | | | | | Doing `Author.joins(:posts).merge(Post.joins(:comments))` does this `SELECT ... INNER JOIN posts ON... LEFT OUTER JOIN comments ON...` instead of doing `SELECT ... INNER JOIN posts ON... INNER JOIN comments ON...`. This behavior is unexpected and makes little sense as, basically, doing `Post.joins(:comments)` means I want posts that have comments. Turning it to a LEFT JOIN means I want posts and join the comments data, if any. We can see this problem directly in the existing tests. The test_relation_merging_with_merged_joins_as_symbols only does joins from posts to comments to ratings while the ratings fixture isn't loaded, but the count is non-zero.
* Refactor making join constraintsRyuta Kamizono2017-05-231-23/+6
| | | | | The only difference between `make_inner_joins` and `make_left_outer_joins` is the `join_type`.
* Remove `node` parameter to `join_constraints`Aaron Patterson2017-03-031-1/+1
| | | | | I don't think we actually need this parameter anymore. Nobody seems to be using it.
* Fix `scopes` implementation on `PolymorphicReflection`Aaron Patterson2017-01-301-1/+1
| | | | `PolymorphicReflection` needs to be custom for handling scope lambdas
* class Foo < Struct.new(:x) creates an extra unneeded anonymous classAkira Matsuda2017-01-131-1/+1
| | | | because Struct.new returns a Class, we just can give it a name and use it directly without inheriting from it
* Add more rubocop rules about whitespacesRafael Mendonça França2016-10-291-9/+9
|
* fixes remaining RuboCop issues [Vipul A M, Xavier Noria]Xavier Noria2016-09-011-2/+2
|
* Ensure that inverse associations are set before running callbacksSean Griffin2016-08-311-3/+5
| | | | | | | | | | | | | | | | | 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/+5
|\ | | | | | | Preserve readonly flag only for readonly association
| * Preserve readonly flag only for readonly associationTakashi Kokubun2016-07-301-1/+3
| | | | | | | | Fixes #24093
* | normalizes indentation and whitespace across the projectXavier Noria2016-08-061-105/+105
| |
* | applies new string literal convention in activerecord/libXavier Noria2016-08-061-3/+3
|/ | | | | The current code base is not uniform. After some discussion, we have chosen to go with double quotes by default.
* Exists shouldn't error when used with `includes`Sean Griffin2016-05-301-2/+4
| | | | | | | | | | | | | | | | Currently `exists?` does some hackery where it assumes that we can join onto anything that we passed to `eager_load` or `includes`, which doesn't work if we are joining onto a polymorphic association. Actually figuring out if we want to include something would require knowledge deep within the join dependency module, which is hard to pull up. The simplest solution is just to pass a flag down that says we're not actually going to try to eager load any of the data. It's not the solution I'd like, but that code really needs to be untangled before we can do much with it. This is another attempt at 6d5b1fd which should address the concerns that led to reverting it in 4ecabed.
* Improve clarity of error message for missing includes and eager_loadJames Wen2016-01-281-1/+1
| | | | relations
* docs, `ActiveRecord::JoinDependency` is not part of the public API.Yves Senn2015-11-071-1/+1
| | | | | | | [ci skip] While `JoinDependency` and `JoinDependency::Aliases` were nodoced, the inner `Table` class made them appear in the API.
* Merge pull request #12071 from Crunch09/outer_joinsSean Griffin2015-10-301-2/+15
|\ | | | | | | added ActiveRecord::Relation#outer_joins
| * added ActiveRecord::Relation#left_outer_joinsFlorian Thomas2015-05-191-2/+15
| | | | | | | | | | | | Example: User.left_outer_joins(:posts) => SELECT "users".* FROM "users" LEFT OUTER JOIN "posts" ON "posts"."user_id" = "users"."id"
* | Merge pull request #18383 from ↵Rafael Mendonça França2015-10-271-1/+2
|\ \ | | | | | | | | | | | | | | | scambra/habtm-with-where-includes-16032-for-master Includes HABTM returns correct size now
| * | Includes HABTM returns correct size now. It's caused by the join dependencySergio Cambra2015-01-071-2/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | only instantiates one HABTM object because the join table hasn't a primary key. Updated commit from @bigxiang commit dbaa837 Fixes #16032. Examples: before: Project.first.salaried_developers.size # => 3 Project.includes(:salaried_developers).first.salaried_developers.size # => 1 after: Project.first.salaried_developers.size # => 3 Project.includes(:salaried_developers).first.salaried_developers.size # => 3
* | | Fix for activerecord join dependency instantiate bugMehmet Emin İNAÇ2015-05-041-6/+5
| |/ |/| | | | | | | | | | | | | | | | | | | | | use only object_id instead parent class and parent id test cases assert_equal use table name in references fix minor problems
* | Merge pull request #16989 from Empact/reload-cache-clearRafael Mendonça França2015-02-201-6/+4
|\ \ | | | | | | | | | Isolate access to @associations_cache and @aggregations_cache to the Associations and Aggregations modules, respectively.
| * | Isolate access to @associations_cache and @aggregations cache to the ↵Ben Woosley2014-09-281-6/+4
| | | | | | | | | | | | | | | | | | | | | | | | Associations and Aggregations modules, respectively. This includes replacing the `association_cache` accessor with a more limited `association_cached?` accessor and making `clear_association_cache` and `clear_aggregation_cache` private.
* | | Fix n+1 query problem when eager loading nil associations (fixes #18312)Sammy Larbi2015-01-031-1/+6
| |/ |/|