aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/relation
Commit message (Collapse)AuthorAgeFilesLines
* Ensure `1 AS one` for SQL Server with calculations.Ken Collins2017-09-221-1/+1
|
* Remove unused explicit delegation to `klass` in `relation`Ryuta Kamizono2017-09-141-2/+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`.
* Don't use `quoted_table_name` in `limited_ids_for`Ryuta Kamizono2017-09-141-1/+3
| | | | | | Because `quoted_table_name` doesn't respect table alias. We should use `arel_attribute` for that, so I added `column_name_from_arel_node` to generate column name from an arel node.
* `quoted_table_name` doesn't respect table aliasRyuta Kamizono2017-09-141-1/+1
| | | | So using `arel_attribute(primary_key).asc` in `batch_order` instead.
* Make `in_batches` queries to preparableRyuta Kamizono2017-09-141-3/+10
| | | | | | | | | | | | | | | | | | | | | | | | Before: ```sql SELECT "posts".* FROM "posts" ORDER BY "posts"."id" ASC LIMIT ? [["LIMIT", 2]] SELECT "posts".* FROM "posts" WHERE "posts"."id" > 2 ORDER BY "posts"."id" ASC LIMIT ? [["LIMIT", 2]] SELECT "posts".* FROM "posts" WHERE "posts"."id" > 4 ORDER BY "posts"."id" ASC LIMIT ? [["LIMIT", 2]] SELECT "posts".* FROM "posts" WHERE "posts"."id" > 6 ORDER BY "posts"."id" ASC LIMIT ? [["LIMIT", 2]] SELECT "posts".* FROM "posts" WHERE "posts"."id" > 8 ORDER BY "posts"."id" ASC LIMIT ? [["LIMIT", 2]] SELECT "posts".* FROM "posts" WHERE "posts"."id" > 10 ORDER BY "posts"."id" ASC LIMIT ? [["LIMIT", 2]] ``` After: ```sql SELECT "posts".* FROM "posts" ORDER BY "posts"."id" ASC LIMIT ? [["LIMIT", 2]] SELECT "posts".* FROM "posts" WHERE "posts"."id" > ? ORDER BY "posts"."id" ASC LIMIT ? [["id", 2], ["LIMIT", 2]] SELECT "posts".* FROM "posts" WHERE "posts"."id" > ? ORDER BY "posts"."id" ASC LIMIT ? [["id", 4], ["LIMIT", 2]] SELECT "posts".* FROM "posts" WHERE "posts"."id" > ? ORDER BY "posts"."id" ASC LIMIT ? [["id", 6], ["LIMIT", 2]] SELECT "posts".* FROM "posts" WHERE "posts"."id" > ? ORDER BY "posts"."id" ASC LIMIT ? [["id", 8], ["LIMIT", 2]] SELECT "posts".* FROM "posts" WHERE "posts"."id" > ? ORDER BY "posts"."id" ASC LIMIT ? [["id", 10], ["LIMIT", 2]] ```
* PERF: Recover `ActiveRecord::pluck` performance.Guo Xiang Tan2017-09-061-14/+17
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | ```ruby require 'active_record' require 'benchmark/ips' 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.timestamps null: false end end attributes = { name: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.', email: 'foobar@email.com' } class User < ActiveRecord::Base; end 1000.times do User.create!(attributes) end Benchmark.ips do |x| x.config(time: 10, warmup: 2) x.report('pluck 1 column') do User.pluck(:id) end x.report('pluck 2 columns') do User.pluck(:id, :email) end x.report('pluck 1 column with scope') do User.where(id: 1000).pluck(:id) end x.report('pluck 2 columns with scope') do User.where(id: 1000).pluck(:id, :email) end end ``` ``` Calculating ------------------------------------- pluck 1 column 122.000 i/100ms pluck 2 columns 74.000 i/100ms pluck 1 column with scope 615.000 i/100ms pluck 2 columns with scope 515.000 i/100ms ------------------------------------------------- pluck 1 column 1.272k (± 3.9%) i/s - 12.810k pluck 2 columns 750.096 (± 3.3%) i/s - 7.548k pluck 1 column with scope 6.074k (± 4.1%) i/s - 60.885k pluck 2 columns with scope 5.158k (± 2.7%) i/s - 52.015k ``` ``` Calculating ------------------------------------- pluck 1 column 126.000 i/100ms pluck 2 columns 78.000 i/100ms pluck 1 column with scope 457.000 i/100ms pluck 2 columns with scope 434.000 i/100ms ------------------------------------------------- pluck 1 column 1.266k (± 2.1%) i/s - 12.726k pluck 2 columns 795.061 (± 3.0%) i/s - 7.956k pluck 1 column with scope 4.660k (± 2.1%) i/s - 46.614k pluck 2 columns with scope 4.355k (± 2.3%) i/s - 43.834k ``` ``` Calculating ------------------------------------- pluck 1 column 126.000 i/100ms pluck 2 columns 78.000 i/100ms pluck 1 column with scope 539.000 i/100ms pluck 2 columns with scope 481.000 i/100ms ------------------------------------------------- pluck 1 column 1.308k (± 3.4%) i/s - 13.104k pluck 2 columns 798.604 (± 2.8%) i/s - 8.034k pluck 1 column with scope 5.530k (± 3.4%) i/s - 55.517k pluck 2 columns with scope 4.914k (± 2.7%) i/s - 49.543k ``` ``` Calculating ------------------------------------- pluck 1 column 139.000 i/100ms pluck 2 columns 79.000 i/100ms pluck 1 column with scope 580.000 i/100ms pluck 2 columns with scope 526.000 i/100ms ------------------------------------------------- pluck 1 column 1.337k (± 3.0%) i/s - 13.483k pluck 2 columns 806.776 (± 2.7%) i/s - 8.137k pluck 1 column with scope 5.924k (± 4.1%) i/s - 59.160k pluck 2 columns with scope 5.276k (± 3.1%) i/s - 53.126k ```
* Merge pull request #30377 from keepcosmos/delegate-missing-methodsMatthew Draper2017-08-311-2/+2
|\ | | | | Delegate :rindex, :slice, :rotate(missing) to 'records'
| * Delegate :rindex, :slice, :rotate to 'records'keepcosmos2017-08-241-2/+2
| |
* | Should be appear deprecation message for every call (#29649)Ryuta Kamizono2017-08-271-8/+0
| | | | | | Context: https://github.com/rails/rails/pull/29619#discussion_r125158589
* | Should work inverse association when eager loadingRyuta Kamizono2017-08-251-10/+1
|/ | | | | | | This regression was caused by caa178c1. The block for `set_inverse_instance` should also be passed to join dependency. Fixes #30402.
* Merge pull request #30169 from awortham/awortham/awortham/fix-sql-distinct-bugRafael Mendonça França2017-08-141-0/+3
|\ | | | | | | Ensure sum honors distinct on has_many through
| * Ensure sum honors distinct on has_many throughAaron Wortham2017-08-141-0/+3
| | | | | | | | | | | | When using a has_many through relation and then summing an attribute the distinct was not being used. This will ensure that when summing an attribute, the number is only used once when distinct has been used.
* | Completes ActiveRecord::Batches.find_each example [ci skip]Marc Rendl Ignacio2017-08-131-1/+6
|/ | | | | The previous paragraph mentions that you can hand off the same processing queue to multiple workers. This completes the following example below it.
* Merge pull request #30126 from ↵Rafael França2017-08-111-2/+2
|\ | | | | | | | | chopraanmol1/support_for_has_many_and_has_one_for_where_relation Fixed query building when relation is passed for has one or has many association in where
| * Changed join_fk private method to join_foreign_key public methodchopraanmol12017-08-091-1/+1
| |
| * Currently if relation object are passed to where condition for has one or ↵chopraanmol12017-08-081-2/+2
| | | | | | | | | | | | has many association wrong set of primary key and foreign key are selected. Changed code to use 'join' primary key and foreign key over 'association' primary key and foreign key.
* | Merge pull request #29720 from gaurish/ar_find_error_message_improvementRafael França2017-08-111-3/+3
|\ \ | |/ |/| Return Not found Ids in ActiveRecord::NotFound
| * Return Not found Ids in ActiveRecord::NotFoundGaurish Sharma2017-07-291-3/+3
| | | | | | | | | | This builds on top of 15e2da656f41af0124f7577858536f3b65462ad5. now it also returns exact Ids which were not found which will be debugging simple.
* | Merge pull request #29914 from ↵Matthew Draper2017-08-022-19/+22
|\ \ | | | | | | | | | | | | kamipo/relation_merger_should_not_fill_empty_values `Relation::Merger` should not fill `values` with empty values
| * | `get_value` and `set_value` in `Relation` are no longer used externallyRyuta Kamizono2017-07-251-9/+10
| | |
| * | `Relation::Merger` should not fill `values` with empty valuesRyuta Kamizono2017-07-251-10/+12
| | | | | | | | | | | | | | | | | | | | | | | | | | | Currently `Relation#merge` will almost fill `values` with empty values (e.g. `other.order_values` is always true, it should be `other.order_values.any?`). This means that `Relation#merge` always changes `values` even if actually `values` is nothing changed. This behavior will makes `Relation#empty_scope?` fragile. So `Relation#merge` should avoid unnecessary changes.
* | | Edits following the reviewsMaxime Lapointe2017-07-281-6/+8
| | |
* | | Avoid duplicate clauses when using #orMaxime Lapointe2017-07-251-9/+16
| |/ |/| | | | | | | Condenses the clauses that are common to both sides of the OR and put them outside, before the OR This fix the current behavior where the number of conditions is exponential based on the number of times #or is used.
* | Merge pull request #29941 from kamipo/remove_single_element_array_preprocessSean Griffin2017-07-251-1/+1
|\ \ | | | | | | Remove single element array preprocess
| * | Remove single element array preprocessRyuta Kamizono2017-07-261-1/+1
| | | | | | | | | | | | | | | Since 213796f, array predicate handler supports making binds, so the preprocess is no longer needed.
* | | Allow `Relation#or` to accept a relation with different `references`Sean Griffin2017-07-251-1/+2
|/ / | | | | | | | | | | | | | | | | Note that the two relations must still have the same `includes` values (which is the only time `references` actually does anything). It makes sense for us to allow this, as `references` is called implicitly when passing a hash to `where`. Fixes #29411
* | Merge pull request #29934 from kamipo/remove_join_informationSean Griffin2017-07-251-5/+2
|\ \ | | | | | | Remove useless `JoinInformation`
| * | Remove useless `JoinInformation`Ryuta Kamizono2017-07-251-5/+2
| | | | | | | | | | | | | | | Since 213796f removed `binds`, `JoinInformation` only contain `joins`. So it is enough to return `joins` simply.
* | | Remove unused `queries_predicates`Ryuta Kamizono2017-07-251-12/+0
|/ / | | | | | | Since 213796f, `queries_predicates` is no longer used.
* | Merge pull request #29848 from kamipo/fix_distinct_count_with_order_and_limitRafael França2017-07-241-10/+19
|\ \ | |/ |/| Fix `COUNT(DISTINCT ...)` with `ORDER BY` and `LIMIT`
| * Should keep the table name qualified `*` for distinct subqueryRyuta Kamizono2017-07-221-1/+1
| |
| * Fix `COUNT(DISTINCT ...)` with `ORDER BY` and `LIMIT`Ryuta Kamizono2017-07-221-10/+19
| | | | | | | | | | | | | | 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.
* | Fix build failures on PGSean Griffin2017-07-241-0/+4
| | | | | | | | | | | | | | | | | | | | Honestly I don't think the tests that are fixed by this change should have been merged. Passing a range or an array to `where` has a special meaning. We need to solve the problem more concretely without overriding the behavior that is present for *every* other type. However, the damage has been done. These changes were in 5.1, so we need a deprecation cycle to remove it.
* | Refactor Active Record to let Arel manage bind paramsSean Griffin2017-07-2412-231/+109
|/ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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-1921-0/+42
|
* Merge branch 'master' into fix_unscope_where_column_with_orSean Griffin2017-07-184-14/+14
|\
| * Fix `JoinDependency` with using a custom tableRyuta Kamizono2017-07-183-7/+6
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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 ? ```
| * Fix `where` with a custom tableRyuta Kamizono2017-07-181-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Without this fix, SELECT clause doesn't use a custom table alias name: ``` % ARCONN=sqlite3 be ruby -w -Itest test/cases/relations_test.rb -n test_using_a_custom_table_affects_the_wheres Using sqlite3 Run options: -n test_using_a_custom_table_affects_the_wheres --seed 31818 E Error: RelationTest#test_using_a_custom_table_affects_the_wheres: ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: posts: SELECT "posts".* FROM "posts" "omg_posts" WHERE "omg_posts"."title" = ? LIMIT ? ```
| * Remove useless `arel_engine`Ryuta Kamizono2017-07-171-1/+1
| | | | | | | | | | | | | | | | | | | | | | `arel_engine` is only used in `raise_record_not_found_exception!` to use `engine.connection` (and `connection.visitor`) in `arel.where_sql`. https://github.com/rails/arel/blob/v8.0.0/lib/arel/select_manager.rb#L183 But `klass.connection` will work as expected even if not using `arel_engine` (described by `test_connection`). So `arel_engine` is no longer needed.
| * Fix code formatting for QueryMethods#selectRobin Dupret2017-07-161-2/+3
| | | | | | | | [ci skip]
| * Fix `create_with` using both string and symbolRyuta Kamizono2017-07-162-3/+3
| | | | | | | | | | | | | | 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.
* | Bugfix: unscope(where: [columns]) would not remove the correct binds sometimesMaxime Lapointe2017-07-131-1/+2
|/ | | | | Post.where(id: 1).or(Post.where(id: 2)).where(foo: 3).unscope(where: :foo).where_clause.binds.map(&:value) Would return [2, 3] instead of the expected [1,2]
* Skip query cache for in_batches and friendsEugene Kenny2017-07-064-6/+12
| | | | | | | | | | | 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.
* Merge branch 'master' into require_relative_2017Xavier Noria2017-07-021-2/+2
|\
| * Revert "Merge pull request #29540 from kirs/rubocop-frozen-string"Matthew Draper2017-07-0221-21/+0
| | | | | | | | | | This reverts commit 3420a14590c0e6915d8b6c242887f74adb4120f9, reversing changes made to afb66a5a598ce4ac74ad84b125a5abf046dcf5aa.
| * Merge pull request #29540 from kirs/rubocop-frozen-stringMatthew Draper2017-07-0221-0/+21
| |\ | | | | | | | | | Enforce frozen string in Rubocop
| | * Enforce frozen string in RubocopKir Shatrov2017-07-0121-0/+21
| | |
| * | Merge pull request #29506 from pat/frozen-string-literalsMatthew Draper2017-07-021-2/+2
| |\ \ | | |/ | |/| | | | Make ActiveSupport frozen-string-literal friendly.
| | * Make ActiveRecord frozen string literal friendly.Pat Allan2017-06-201-2/+2
| | |
* | | [Active Record] require => require_relativeAkira Matsuda2017-07-015-15/+15
|/ /