aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/relations_test.rb
Commit message (Collapse)AuthorAgeFilesLines
* #create_or_find_by/!: add more tests and fix docs (#34653)Bogdan2018-12-081-0/+39
| | | | | | | | * `#create_or_find_by/!`: add more tests * Fix docs of `create_or_find_by` This method uses `find_by!` internally.
* Generate delegation methods to named scope in the definition timeRyuta Kamizono2018-10-091-0/+10
| | | | | | | | | | | | | | | | | | | The delegation methods to named scope are defined when `method_missing` is invoked on the relation. Since #29301, the receiver in the named scope is changed to the relation like others (e.g. `default_scope`, etc) for consistency. Most named scopes would be delegated from relation by `method_missing`, since we don't allow scopes to be defined which conflict with instance methods on `Relation` (#31179). But if a named scope is defined with the same name as any method on the `superclass` (e.g. `Kernel.open`), the `method_missing` on the relation is not invoked. To address the issue, make the delegation methods to named scope is generated in the definition time. Fixes #34098.
* Extract `{update,delete}_all_test.rb` from `persistence_test.rb` and ↵Ryuta Kamizono2018-09-161-180/+0
| | | | | | | | `relations_test.rb` `persistence_test.rb` and `relations_test.rb` have too many lines, so I'd like to extract relation around tests to dedicated files before newly test added.
* Fixes #33610Darwin D Wu2018-09-111-0/+11
| | | | | | | | | | | | In order to avoid double assignments of nested_attributes for `has_many` relations during record initialization, nested_attributes in `create_with` should not be passed into `klass.new` and have them populate during `initialize_internals_callback` with scope attributes. However, `create_with` keys should always have precedence over where clauses, so if there are same keys in both `create_with` and `where_values_hash`, the value in `create_with` should be the one that's used.
* Just delegate `update` with ids on a relation to `klass.update`Ryuta Kamizono2018-08-311-0/+18
| | | | | | | | | | | | This restores an ability that `update` with ids on a relation which is described at https://github.com/rails/rails/issues/33470#issuecomment-411203013. I personally think that the `update` with two arguments on a relation is not a designed feature, since that is totally not using a relation state, and also is not documented. But removing any feature should not be suddenly happened in a stable version even if that is not documented.
* Avoid extra scoping when using `Relation#update`Ryuta Kamizono2018-07-311-0/+4
| | | | | | | | | | | | | | Since 9ac7dd4, class level `update`, `destroy`, and `delete` were placed in the `Persistence` module as class methods. But `Relation#update` without passing ids which was introduced at #11898 is not a class method, and it was caused the extra scoping regression #33470. I moved the relation method back into the `Relation` to fix the regression. Fixes #33470.
* Merge pull request #31513 from fatkodima/relation-touch_allRafael França2018-04-201-1/+46
|\ | | | | Add `touch_all` method to `ActiveRecord::Relation`
| * Add `touch_all` method to `ActiveRecord::Relation`fatkodima2018-04-131-1/+46
| |
* | Replace `assert !` with `assert_not`Daniel Colson2018-04-191-8/+8
|/ | | | | This autocorrects the violations after adding a custom cop in 3305c78dcd.
* Merge pull request #32005 from maschwenk/ar-distinct-order-count-regressionRyuta Kamizono2018-02-271-0/+6
| | | | Active Record distinct & order #count regression
* Add #create_or_find_by to lean on unique constraints (#31989)David Heinemeier Hansson2018-02-141-0/+29
| | | Add #create_or_find_by to lean on unique constraints
* Remove extra whitespaceDaniel Colson2018-01-251-14/+14
|
* Use assert_empty and assert_not_emptyDaniel Colson2018-01-251-4/+4
|
* Use assert_predicate and assert_not_predicateDaniel Colson2018-01-251-62/+62
|
* Use respond_to test helpersDaniel Colson2018-01-251-5/+5
|
* Fix `count(:all)` with eager loading and having an order other than the ↵Ryuta Kamizono2018-01-251-0/+6
| | | | | | | | | | | | | | | | | | | 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-241-1/+5
| | | | | | | | | | | | 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 `count(:all)` to correctly work `distinct` with custom SELECT listRyuta Kamizono2017-12-201-0/+6
| | | | | | | | | | | | | | 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.
* Remove outdated comments [ci skip]Ryuta Kamizono2017-12-191-4/+0
|
* Using subselect for `delete_all` with `limit` or `offset`Ryuta Kamizono2017-12-191-2/+0
| | | | | | 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.
* Fix `scope_for_create` to do not lose polymorphic associationsRyuta Kamizono2017-12-081-0/+9
| | | | | | | | | | | | This regression was caused at 213796fb due to polymorphic predicates are combined by `Arel::Nodes::And`. But I'd like to keep that combined because it would help inverting polymorphic predicates correctly (e9ba12f7), and we can collect equality nodes regardless of combined by `Arel::Nodes::And` (`a AND (b AND c) AND d` == `a AND b AND c AND d`). This change fixes the regression to collect equality nodes in `Arel::Nodes::And` as well. Fixes #31338.
* Merge pull request #27947 from mastahyeti/unsafe_raw_sqlMatthew Draper2017-11-141-19/+19
|\ | | | | | | Disallow raw SQL in dangerous AR methods
| * deal with Array arguments to #orderBen Toews2017-11-091-3/+3
| |
| * convert order arg to string before checking if we can reverse itBen Toews2017-11-091-2/+2
| |
| * use << instead of #concat in #reverse_sql_order because we might be working ↵Ben Toews2017-11-091-1/+1
| | | | | | | | with Arel SQL literator which overrides #concat
| * try using regexesBen Toews2017-11-091-26/+23
| |
| * allow table name and direction in string order argBen Toews2017-11-091-33/+33
| |
| * work around deprecation warnings in a bunch of testsBen Toews2017-11-091-61/+64
| |
* | Fix random CI failure due to non-deterministic sorting orderRyuta Kamizono2017-11-101-1/+1
| | | | | | | | https://travis-ci.org/rails/rails/jobs/300163487#L1974
* | Ensure `apply_join_dependency` for subqueries in `from` and `where`Ryuta Kamizono2017-11-101-0/+12
| | | | | | | | Fixes #21577.
* | Set counter caches to correct values in fixturesBogdan Gusiev2017-11-081-9/+9
|/
* All test cases for `exists?` places in `finder_test.rb` to ease to find the ↵Ryuta Kamizono2017-10-091-26/+0
| | | | test cases
* Ensure `AliasTracker` respects a custom table nameRyuta Kamizono2017-09-301-4/+8
|
* Add test case for `arel_attribute` with a custom tableRyuta Kamizono2017-09-271-0/+4
| | | | Since #29301, `arel_attribute` respects a custom table name.
* Treat `Set` as an `Array` in `Relation#where`Sean Griffin2017-09-261-0/+13
| | | | | | | | I do not want to set the expectation that any enumerable object should behave this way, but this case in particular comes up frequently enough that I'm caving on this one. Fixes #30684.
* Refactor Active Record to let Arel manage bind paramsSean Griffin2017-07-241-46/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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.
* Merge pull request #29867 from koic/fix_a_failed_ar_test_when_using_oracleKasper Timm Hansen2017-07-231-2/+10
|\ | | | | Fix a failued AR test when using OracleAdapter
| * Fix a failued AR test when using OracleAdapterKoichi ITO2017-07-201-2/+10
| |
* | Merge pull request #29732 from kirs/frozen-activerecordRafael França2017-07-211-0/+2
|\ \ | | | | | | Use frozen-string-literal in ActiveRecord
| * | Use frozen-string-literal in ActiveRecordKir Shatrov2017-07-191-0/+2
| |/
* / Revert "Extract `bind_param` and `bind_attribute` into `ActiveRecord::TestCase`"Sean Griffin2017-07-211-7/+3
|/ | | | | | | | This reverts commit b6ad4052d18e4b29b8a092526c2beef013e2bf4f. This is not something that the majority of Active Record should be testing or care about. We should look at having fewer places rely on these details, not make it easier to rely on them.
* Fix `JoinDependency` with using a custom tableRyuta Kamizono2017-07-181-5/+14
| | | | | | | | | | | | | | | 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 ? ```
* Merge pull request #29801 from kamipo/extract_null_relation_testSean Griffin2017-07-171-118/+0
|\ | | | | Extract `NullRelationTest` from `RelationTest`
| * Extract `NullRelationTest` from `RelationTest`Ryuta Kamizono2017-07-151-118/+0
| | | | | | | | | | `test/cases/relations_test.rb` file has too much lines (2000 over). So I extracted `NullRelationTest` to the dedicated file.
* | Merge pull request #29799 from ↵Sean Griffin2017-07-171-8/+0
|\ \ | | | | | | | | | | | | kamipo/remove_outdated_test_scoped_responds_to_delegated_methods Remove outdated `test_scoped_responds_to_delegated_methods`
| * | Remove outdated `test_scoped_responds_to_delegated_methods`Ryuta Kamizono2017-07-151-8/+0
| |/ | | | | | | | | | | | | | | This test was added at 74ed123 to ensure `respond_to?` delegate methods to `Array` and `arel_table`. But array method delegation was removed at 9d79334 in favor of including `Enumerable`. And now `Relation` has `insert`, `update`, and `delete` methods (63480d2, 8d31c9f, d5f9173). So this delegation test is already outdated.
* | Fix `where` with a custom tableRyuta Kamizono2017-07-181-4/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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 ? ```
* | 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-0/+42
| | | | | | | | | | | 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.