aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/relation.rb
Commit message (Collapse)AuthorAgeFilesLines
* Ensure to calculate column aliases after all table aliases are constructedRyuta Kamizono2018-06-191-11/+11
| | | | | | | | | | | | | | | | | 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 `touch` option to behave consistently with `Persistence#touch` methodRyuta Kamizono2018-06-181-4/+1
| | | | | | | | | | | | | | | | `touch` option was added to `increment!` (#27660) and `update_counters` (#26995). But that option behaves inconsistently with `Persistence#touch` method. If `touch` option is passed attribute names, it won't update update_at/on attributes unlike `Persistence#touch` method. Due to changed from `Persistence#touch` to `increment!` with `touch` option, #31405 has a regression that `counter_cache` with `touch` option which is passed attribute names won't update update_at/on attributes. I think that the inconsistency is not intended. To get back consistency, ensure that `touch` option updates update_at/on attributes.
* Fix typo in the `touch_all` doc [ci skip]Ryuta Kamizono2018-04-211-4/+4
|
* Merge pull request #31513 from fatkodima/relation-touch_allRafael França2018-04-201-0/+37
|\ | | | | Add `touch_all` method to `ActiveRecord::Relation`
| * Add `touch_all` method to `ActiveRecord::Relation`fatkodima2018-04-131-0/+37
| |
* | Don't expose `Relation#preload_associations` in the docRyuta Kamizono2018-04-201-1/+1
|/ | | | | | | The extracted method is used for `CollectionCacheAssociationLoading`, still not public API. [ci skip]
* don't check for immutability when setting skip_preloading as it doesn't ↵Lachlan Sylvester2018-04-121-2/+3
| | | | effect the arel and the arel may already be generated by fresh_when
* Deprecate accessibility of private/protected class methods in named scopeRyuta Kamizono2018-03-301-0/+2
|
* Bring back private class methods accessibility in named scopeRyuta Kamizono2018-03-271-0/+7
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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.
* Only preload misses on multifetch cacheLachlan Sylvester2018-03-061-8/+13
|
* Add #create_or_find_by to lean on unique constraints (#31989)David Heinemeier Hansson2018-02-141-12/+42
| | | Add #create_or_find_by to lean on unique constraints
* Rdoc formatting fix: <b> instead of MD-style asterisksT.J. Schuck2018-02-141-1/+1
| | | | [ci skip]
* Avoid passing unnecessary arguments to relationDaniel Colson2018-01-241-1/+1
| | | | | | | | | | | | 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?
* Use `apply_join_dependency` instead of meaningless named ↵Ryuta Kamizono2018-01-111-2/+2
| | | | | | | | | | `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.
* Using subselect for `delete_all` with `limit` or `offset`Ryuta Kamizono2017-12-191-4/+4
| | | | | | Arel doesn't support subselect generation for DELETE unlike UPDATE yet, but we already have that generation in connection adapters. We can simply use the subselect generated by that one.
* Using subselect generated by the connection adapter for `update_all` with ↵Ryuta Kamizono2017-12-191-1/+1
| | | | | | | | | | | `offset` Most RDBMS (except SQLite) requires subselect for UPDATE with OFFSET, but Arel doesn't support executable subselect generation for MySQL's UPDATE yet. We need to use the subselect generated by the connection adapter for now, it works well. Fixes #30148.
* Fix inheritance object creation from relationRyuta Kamizono2017-12-131-8/+18
| | | | | | | | | | We need to pass scope attributes to `klass.new` to detect subclass. Otherwise `subclass_from_attributes` can't detect subclass which is had in scope attributes. Fixes #18062. Closes #18227. Closes #30720.
* Make `sanitize_sql_` methods publicyuuji.yaginuma2017-12-131-1/+1
| | | | | | | | 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.
* Revert "Merge pull request #31006 from ↵eileencodes2017-11-261-1/+0
| | | | | | | | | | | rails/kamipo/ordinal_methods_should_respect_loaded_records" This reverts commit 0f79ab91150b4cdb6c018530978a3395962c7a02, reversing changes made to d575f7f2e737739302a0e8210d01c10f5d4e2c35. This PR philosophically conflicts with #30800 and Matthew thinks we should hold off merging this until we find concensus. Reverting since we're about to cut a release for 5.2.
* Merge pull request #31006 from ↵Eileen M. Uchitelle2017-11-251-0/+1
|\ | | | | | | | | rails/kamipo/ordinal_methods_should_respect_loaded_records Ordinal methods should respect loaded records
| * Ordinal methods should respect loaded recordsRyuta Kamizono2017-10-281-0/+1
| | | | | | | | | | | | We should reset partially loaded `@offsets` cache when latest records has loaded because the cache has been staled and it may not be consistent with latest records.
* | Avoid creating extra `relation` and `build_arel` in `_create_record` and ↵Ryuta Kamizono2017-11-171-61/+0
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | `_update_record` (#29999) Currently `_create_record` and `_update_record` in `Persistence` are creating extra `unscoped` and calling `build_arel` in the relation. But `compile_insert` and `compile_update` can be done without those expensive operation for `SelectManager` creation. So I moved the implementation to `Persistence` to avoid creating extra relation and refactored to avoid calling `build_arel`. https://gist.github.com/kamipo/8ed73d760112cfa5f6263c9413633419 Before: ``` Warming up -------------------------------------- _update_record 150.000 i/100ms Calculating ------------------------------------- _update_record 1.548k (±12.3%) i/s - 7.650k in 5.042603s ``` After: ``` Warming up -------------------------------------- _update_record 201.000 i/100ms Calculating ------------------------------------- _update_record 2.002k (±12.8%) i/s - 9.849k in 5.027681s ``` 30% faster for STI classes.
* | Consolidate duplicated `to_ary`/`to_a` definitions in `Relation` and ↵Ryuta Kamizono2017-11-101-1/+2
| | | | | | | | `CollectionProxy`
* | Ensure `apply_join_dependency` for `update_all` and `delete_all` if ↵Ryuta Kamizono2017-11-061-0/+10
|/ | | | | | | | | | eager-loading is needed If a relation has eager-loading values, `count` and `exists?` works properly, but `update_all` and `delete_all` doesn't work due to missing `apply_join_dependency`. It should be applied to work consistently. Fixes #28863.
* Joined tables in association scope doesn't use the same aliases with the ↵Ryuta Kamizono2017-10-091-1/+2
| | | | | | | | | 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-0/+4
| | | | | This is preparation to respect parent relation's alias tracking for fixing #30681.
* Merge pull request #30619 from ↵Eileen M. Uchitelle2017-09-201-4/+4
|\ | | | | | | | | jagthedrummer/jeremy/instrumentation-payload-names Update payload names for `sql.active_record` instrumentation to be more descriptive.
| * Update payload names for `sql.active_record` to be more descriptive.Jeremy Green2017-09-201-4/+4
| | | | | | | | Fixes #30586.
* | Place class level `update`, `destroy`, and `delete` in ↵Ryuta Kamizono2017-09-181-94/+0
|/ | | | | | | | | | `Persistence::ClassMethods` The docs are obviously for class level `update`, `destroy`, and `delete`. It should be placed in `Persistence::ClassMethods` rather than `Relation`. And also, these methods are not dependent on relation. So it is not needed to delegate to `all` (plus, `klass.find` is faster than `relation.find`).
* Remove unused explicit delegation to `klass` in `relation`Ryuta Kamizono2017-09-141-1/+1
| | | | | | | It is only used `primary_key` and `connection` in the internal, so it is not needed to delegate others to `klass` explicitly. This doesn't change public behavior because `relation` will delegate missing method to `klass`.
* Should work inverse association when eager loadingRyuta Kamizono2017-08-251-2/+14
| | | | | | | This regression was caused by caa178c1. The block for `set_inverse_instance` should also be passed to join dependency. Fixes #30402.
* Restore `to_sql` to return only SQL (#29945)Ryuta Kamizono2017-08-181-2/+1
| | | | Because `to_sql` is public API. I introduced `to_sql_and_binds` internal API to return SQL and binds.
* Use `predicate_builder.build_bind_attribute` wherever possibleRyuta Kamizono2017-07-281-2/+2
| | | | For less duplicated code.
* Merge pull request #29848 from kamipo/fix_distinct_count_with_order_and_limitRafael França2017-07-241-0/+4
|\ | | | | Fix `COUNT(DISTINCT ...)` with `ORDER BY` and `LIMIT`
| * Fix `COUNT(DISTINCT ...)` with `ORDER BY` and `LIMIT`Ryuta Kamizono2017-07-221-0/+4
| | | | | | | | | | | | | | Since #26972, `ORDER BY` is kept if `LIMIT` is presented for performance. But in most SQL servers (e.g. PostgreSQL, SQL Server, etc), `ORDER BY` expressions must appear in select list for `SELECT DISTINCT`. We should not replace existing select list in that case.
* | Refactor Active Record to let Arel manage bind paramsSean Griffin2017-07-241-17/+11
|/ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | A common source of bugs and code bloat within Active Record has been the need for us to maintain the list of bind values separately from the AST they're associated with. This makes any sort of AST manipulation incredibly difficult, as any time we want to potentially insert or remove an AST node, we need to traverse the entire tree to find where the associated bind parameters are. With this change, the bind parameters now live on the AST directly. Active Record does not need to know or care about them until the final AST traversal for SQL construction. Rather than returning just the SQL, the Arel collector will now return both the SQL and the bind parameters. At this point the connection adapter will have all the values that it had before. A bit of this code is janky and something I'd like to refactor later. In particular, I don't like how we're handling associations in the predicate builder, the special casing of `StatementCache::Substitute` in `QueryAttribute`, or generally how we're handling bind value replacement in the statement cache when prepared statements are disabled. This also mostly reverts #26378, as it moved all the code into a location that I wanted to delete. /cc @metaskills @yahonda, this change will affect the adapters Fixes #29766. Fixes #29804. Fixes #26541. Close #28539. Close #24769. Close #26468. Close #26202. There are probably other issues/PRs that can be closed because of this commit, but that's all I could find on the first few pages.
* Use frozen-string-literal in ActiveRecordKir Shatrov2017-07-191-0/+2
|
* Fix unscoping `default_scope` in STI associationsRyuta Kamizono2017-07-191-0/+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`.
* Don't cache `scope_for_create`Ryuta Kamizono2017-07-161-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | I investigated where `scope_for_create` is reused in tests with the following code: ```diff --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -590,6 +590,10 @@ def where_values_hash(relation_table_name = table_name) end def scope_for_create + if defined?(@scope_for_create) && @scope_for_create + puts caller + puts "defined" + end @scope_for_create ||= where_values_hash.merge!(create_with_value.stringify_keys) end ``` It was hit only `test_scope_for_create_is_cached`. This means that `scope_for_create` will not be reused in normal use cases. So we can remove caching `scope_for_create` to respect changing `where_clause` and `create_with_value`.
* Merge pull request #29809 from kamipo/remove_unused_ivarsKasper Timm Hansen2017-07-161-2/+1
|\ | | | | Remove unused `@last`, `@order_clause`, and `@join_dependency`
| * Remove unused `@last`, `@order_clause`, and `@join_dependency`Ryuta Kamizono2017-07-161-2/+1
| | | | | | | | | | | | Using `@last` and `@order_clause` was removed at 8bb5274 and 90d1524. `@join_dependency` was added at b959950 but it is unused in the first place.
* | Fix `create_with` using both string and symbolRyuta Kamizono2017-07-161-1/+1
|/ | | | | | | This is related with #27680. Since `where_values_hash` keys constructed by `where` are string, so we need `stringify_keys` to `create_with_value` before merging it.
* Skip query cache for in_batches and friendsEugene Kenny2017-07-061-13/+25
| | | | | | | | | | | The `find_each`, `find_in_batches` and `in_batches` APIs usually operate on large numbers of records, where it's preferable not to load them all into memory at once. If the query cache is enabled, it will hold onto the query results until the end of the execution context (request/job), which means the memory used is still proportional to the total number of records. These queries are typically not repeated, so the query cache isn't desirable here.
* 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.
* Enforce frozen string in RubocopKir Shatrov2017-07-011-0/+1
|
* Fix to scoping is correctly restoredRyuta Kamizono2017-06-291-1/+1
| | | | | | | | This regression was caused by #23004. If STI subclass is using scoping in parent class scoping, `current_scope` in subclass is never restored. I fixed to restore `current_scope` to previous value correctly.
* `Relation#locked?` should not build arelRyuta Kamizono2017-06-201-1/+1
|
* Merge pull request #29003 from kamipo/delegate_ast_and_locked_to_arel_explicitlyMatthew Draper2017-05-281-0/+1
|\ | | | | Delegate `ast` and `locked` to `arel` explicitly
| * Delegate `ast` and `locked` to `arel` explicitlyRyuta Kamizono2017-05-061-0/+1
| | | | | | | | | | | | | | | | Currently `ast` and `locked` are used in the internal but delegating to `arel` is depend on `method_missing`. If a model class is defined these methods, `select_all` will be broken. It should be delegated to `arel` explicitly.
* | Merge pull request #29237 from mohitnatoo/arel-update-docRafael França2017-05-261-3/+3
|\ \ | | | | | | [ci skip] Changed sentence formation for ActiveRecordRelation#update