aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/relation
Commit message (Collapse)AuthorAgeFilesLines
...
* Enable Start/EndWith and RegexpMatch copsBart de Water2018-07-281-1/+1
| | | | | In cases where the MatchData object is not used, this provides a speed-up: https://github.com/JuanitoFatas/fast-ruby/#stringmatch-vs-stringmatch-vs-stringstart_withstringend_with-code-start-code-end
* don't impose primary key order if limit() is definedBrian Christian2018-07-191-1/+1
|
* Revert "Short circuit the scoping delegation for `relation.all`"Ryuta Kamizono2018-07-191-1/+0
| | | | | | | This reverts commit eb807384c81a6e086b17a576755e992e6c4c685e. If the current scope is affected by the `unscoped` block, `all` won't be the same with `spawn`.
* Use `construct_join_dependency` in all placesRyuta Kamizono2018-07-033-13/+6
|
* Ensure to calculate column aliases after all table aliases are constructedRyuta Kamizono2018-06-193-16/+12
| | | | | | | | | | | | | | | | | 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.
* Fix GROUP BY queries to apply LIMIT/OFFSET after aggregationsRyuta Kamizono2018-06-071-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | If `eager_loading` is true, `apply_join_dependency` force applies LIMIT/OFFSET before JOINs by `limited_ids_for` to keep parent records count. But for aggregation queries, LIMIT/OFFSET should be applied after aggregations the same as SQL semantics. And also, we could not replace SELECT list by `limited_ids_for` when a query has a GROUP BY clause. It had never been worked since it will causes generating invalid SQL for MySQL, PostgreSQL, and probably most backends. ``` % ARCONN=postgresql be ruby -w -Itest test/cases/calculations_test.rb -n test_group_by_with_limit Using postgresql Run options: -n test_group_by_with_limit --seed 20925 # Running: E Error: CalculationsTest#test_group_by_with_limit: ActiveRecord::StatementInvalid: PG::GroupingError: ERROR: column "posts.id" must appear in the GROUP BY clause or be used in an aggregate function LINE 1: SELECT DISTINCT "posts"."id", "posts"."type" AS alias_0 FRO... ^ : SELECT DISTINCT "posts"."id", "posts"."type" AS alias_0 FROM "posts" LEFT OUTER JOIN "comments" ON "comments"."post_id" = "posts"."id" GROUP BY "posts"."type" ORDER BY "posts"."type" ASC LIMIT $1 ``` Fixes #8103. Closes #27249.
* Fix force equality checking not to break the serialized attribute with ArrayRyuta Kamizono2018-06-061-1/+0
| | | | Context: https://github.com/rails/rails/commit/43ef00e5d7a55ad79bc840276d33cb70f1f5dde5#commitcomment-29256140
* Make force equality checking more strictly not to allow serialized attributeRyuta Kamizono2018-05-251-5/+7
| | | | | | | | | | | Since #26074, introduced force equality checking to build a predicate consistently for both `find` and `create` (fixes #27313). But the assumption that only array/range attribute have subtype was wrong. We need to make force equality checking more strictly not to allow serialized attribute. Fixes #32761.
* `get_value` needs to be a public methodGraham Turner2018-04-251-4/+5
| | | | | | Adds test case for failing issue Moves set_value back to protected
* Fix relation merging with skip_query_cache!James Williams2018-04-191-2/+2
|
* Merge pull request #32355 from kamipo/delegate_to_klass_in_a_scopeRafael França2018-04-061-0/+5
|\ | | | | Bring back private class methods accessibility in named scope
| * Deprecate accessibility of private/protected class methods in named scopeRyuta Kamizono2018-03-301-2/+4
| |
| * Bring back private class methods accessibility in named scopeRyuta Kamizono2018-03-271-0/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The receiver in a scope was changed from `klass` to `relation` itself for all scopes (named scope, default_scope, and association scope) behaves consistently. In addition. Before 5.2, if both an AR model class and a Relation instance have same named methods (e.g. `arel_attribute`, `predicate_builder`, etc), named scope doesn't respect relation instance information. For example: ```ruby class Post < ActiveRecord::Base has_many :comments1, class_name: "RecentComment1" has_many :comments2, class_name: "RecentComment2" end class RecentComment1 < ActiveRecord::Base self.table_name = "comments" default_scope { where(arel_attribute(:created_at).gteq(2.weeks.ago)) } end class RecentComment2 < ActiveRecord::Base self.table_name = "comments" default_scope { recent_updated } scope :recent_updated, -> { where(arel_attribute(:updated_at).gteq(2.weeks.ago)) } end ``` If eager loading `Post.eager_load(:comments1, :comments2).to_a`, `:comments1` (default_scope) respects aliased table name, but `:comments2` (using named scope) may not work correctly since named scope doesn't respect relation instance information. See also 801ccab. But this is a breaking change between releases without deprecation. I decided to bring back private class methods accessibility in named scope. Fixes #31740. Fixes #32331.
* | Short circuit the scoping delegation for `relation.all`Ryuta Kamizono2018-03-301-0/+1
|/ | | | | Previously `relation.all` behaves as `relation.scoping { klass.all }`. But it is just enough to `relation.spawn`.
* Merge pull request #32221 from composerinteralia/batch-predicate-builderRyuta Kamizono2018-03-221-10/+16
|\ | | | | Use PredicateBuilder for bind params in Relation::Batches
| * Use PredicateBuilder for bind params in BatchesDaniel Colson2018-03-111-10/+16
| | | | | | | | | | | | Using the PredicateBuilder to build the bind attributes allows Batch to drop its dependency on Relation::QueryAttribute and Arel::Nodes::BindParam
* | Only preload misses on multifetch cacheLachlan Sylvester2018-03-061-0/+5
|/
* Eager loading with polymorphic associations should behave consistentlyRyuta Kamizono2018-03-041-3/+3
| | | | | | | | | | 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.
* Extract all `base_class.name` as `polymorphic_name`Ryuta Kamizono2018-03-041-5/+7
| | | | | | | 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.
* Distinct with order #count regressionMax Schwenk2018-02-251-1/+1
|
* PostgreSQL: Treat infinite values in date like datetime consistentlyRyuta Kamizono2018-02-232-3/+21
| | | | | | | | | | | | | | The values infinity and -infinity are supported by both date and timestamp types. https://www.postgresql.org/docs/current/static/datatype-datetime.html#DATATYPE-DATETIME-SPECIAL-TABLE And also, it can not be known whether a value is infinity correctly unless cast a value. I've added `QueryAttribute#infinity?` to handle that case. Closes #27585.
* Use private attr_readerRyuta Kamizono2018-02-236-13/+6
| | | | | Since #32028, Rails 6 requires Ruby 2.3+. No longer needed workaround for Ruby 2.2 "private attribute?" warning.
* Revert "Delegate `uniq` to `records`"Ryuta Kamizono2018-02-181-1/+1
| | | | | | This reverts commit cf4f05a7d4a2051cf3593bc7c3a6a216e74e797a. Since Rails 6 requires Ruby 2.4.1+.
* Rails 6 requires Ruby 2.3+Jeremy Daer2018-02-172-8/+2
|
* Merge pull request #31133 from ↵Matthew Draper2018-02-111-0/+1
|\ | | | | | | | | mohsen-alizadeh/sanitize_empty_and_nil_parameters_passed_to_select sanitize empty and nil parameters to select #31059
| * sanitize empty and nil parameters to selectMohsen Alizadeh2017-11-121-0/+1
| |
* | Fix formatting of `pick` [ci skip]yuuji.yaginuma2018-02-101-1/+1
| |
* | Add Relation#pick as short-hand for single-value plucks (#31941)David Heinemeier Hansson2018-02-091-0/+18
| | | | | | * Add Relation#pick as short-hand for single-value plucks
* | Avoid extra calls to to_sDaniel Colson2018-01-291-1/+1
| | | | | | | | | | | | | | 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.
* | Fix expanding an array of `composed_of` objects which have multiple mappingsRyuta Kamizono2018-01-292-1/+12
| | | | | | | | | | | | | | | | | | Follow up of #31724. If `composed_of` objects have multiple mappings, array predicate handler can not correctly handle the expanded condition. We need to handle it like polymorphic association objects.
* | Fix `count(:all)` with eager loading and having an order other than the ↵Ryuta Kamizono2018-01-251-1/+8
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | driving table This is a regression caused by 6beb4de. In PostgreSQL, ORDER BY expressions must appear in SELECT list when using DISTINCT. When using `count(:all)` with eager loading, Active Record enforces DISTINCT to count the driving table records only. 6beb4de was caused the regression because `count(:all)` with DISTINCT path no longer removes ORDER BY. We need to ignore ORDER BY when DISTINCT is enforced, otherwise not always generated valid SQL for PostgreSQL. Fixes #31783.
* | Avoid passing unnecessary arguments to relationDaniel Colson2018-01-242-2/+6
| | | | | | | | | | | | | | | | | | | | | | | | 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?
* | Fix relation merger issue with `left_outer_joins`Mehmet Emin INAC2018-01-152-1/+27
| |
* | Merge pull request #28313 from sandrew/masterRyuta Kamizono2018-01-151-2/+3
|\ \ | | | | | | | | | Allow unscoping of left_outer_joins
| * | Allow unscoping of left_outer_joinsAndrei Shaidurov2017-03-061-1/+1
| | |
* | | Don't need to pass `manager` to `convert_join_strings_to_ast`Ryuta Kamizono2018-01-141-2/+2
| | | | | | | | | | | | Because `Relation` already have Arel `table`.
* | | Don't pass garbage args to alias trackerRyuta Kamizono2018-01-141-1/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This is a complete fix to #30995. Originally alias tracker will only track table aliases on `Arel::Nodes::Join`, other args are ignored. Since c5ab6e5, parent aliases hash will be passed then it caused the regression #30995. It is enough to pass list of `Arel::Nodes::Join` simply, not need to pass garbage args which will be ignored.
* | | Use `apply_join_dependency` instead of meaningless named ↵Ryuta Kamizono2018-01-111-24/+11
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | `find_with_associations` `find_with_associations` is meaningless name in this point since it just contain `construct_join_dependency` and `apply_join_dependency`, does not contain finding anything. If `apply_join_dependency` returns `relation` and `join_dependency` then `find_with_associations` is no longer needed.
* | | Make `relation.exists?` more performant when using eager loadingRyuta Kamizono2018-01-111-3/+4
| | | | | | | | | | | | | | | | | | `relation.exists?` just wants to know if there is a result or not, does not need the exact records matched. Therefore, an intermediate SELECT query for eager loading is not necessary.
* | | resolve inconsistencies between first and to_a.first with limitBrian Christian2018-01-091-1/+5
| | |
* | | Make `find_nth_from_last` more performant when using reversible orderRyuta Kamizono2018-01-071-6/+5
| | | | | | | | | | | | | | | We can use `relation.last(index)[-index]` instead of loading all records when using reversible order because `last` will call `reverse_order`.
* | | Fix `last` with `offset` to behave consistently with loaded relationRyuta Kamizono2018-01-071-1/+1
| | | | | | | | | | | | | | | | | | Currently `last` with `offset` behaves incorrectly because `offset` can not be reversed like `limit`. Therefore, `offset` should also be handled like `limit`.
* | | Fix `pluck` with eager loading to respect `offset`Ryuta Kamizono2018-01-071-1/+1
| | |
* | | Partial revert the changing default value of `readonly_value`Ryuta Kamizono2018-01-051-13/+2
| | | | | | | | | | | | | | | This is a partial revert of #26182. There is no reason to change the default value.
* | | Refactor delegating `join_primary_key` instead of `join_keys` and ↵Ryuta Kamizono2018-01-012-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`.
* | | Place args normalization from `left_outer_joins` to `left_outer_joins!`Ryuta Kamizono2017-12-201-4/+2
| | | | | | | | | | | | Like other query bang methods.
* | | Fix `count(:all)` to correctly work `distinct` with custom SELECT listRyuta Kamizono2017-12-201-10/+8
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Currently `count(:all)` with `distinct` doesn't work correctly because SELECT list is always replaced to `*` or primary key in that case even if having custom SELECT list. And also, PostgreSQL has a limitation that ORDER BY expressions must appear in select list for SELECT DISTINCT. Therefore, we should not replace custom SELECT list when using `count(:all)` with `distinct`. Closes #31277.
* | | Using table name qualified column names unless having SELECT list explicitlyRyuta Kamizono2017-12-181-2/+2
| | | | | | | | | | | | | | | | | | Previously table name qualified `*` is used in that case. If it is not qualified with a table name, an ambiguous column name error will occur when using JOINs.
* | | Make `sanitize_sql_` methods publicyuuji.yaginuma2017-12-132-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | Currently, sanitize methods are private. So need `send` to use from outside class. However, sometimes want to use sanitize methods from outside Class when want to generate SQL including multiple tables like search. In order to avoid using `send` in such a case, changed methods to public.
* | | Quote colum_names when building select:Edouard CHIN2017-12-111-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | - #30980 introcuded a change to not use `Arel.star` when model have ignored columns, a query used to look like `SELECT *. FROM developers` whereas now it would like `SELECT column1, column2 FROM developers` - If a column has the same name has a reserved database specific keyword (such as key, where ...) then the query would fail because the names aren't quoted - Quoting almost always happen unless we use a `from` clause in the query https://github.com/rails/rails/blob/9965b98dc0d58a86e10b4343bb6e15e01661a8c3/activerecord/lib/active_record/relation/query_methods.rb#L1052 - This PR cast all columns name to symbols in order for the quoting logic to be picked up https://github.com/rails/rails/blob/9965b98dc0d58a86e10b4343bb6e15e01661a8c3/activerecord/lib/active_record/relation/query_methods.rb#L1054-L1055 - A reproduction script can be found here https://gist.github.com/Edouard-chin/f56d464a0adcb76962afc1a9134a1536