aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/relations_test.rb
Commit message (Collapse)AuthorAgeFilesLines
* Load only needed records on ActiveRecord::Relation#inspectHendy Tanata2017-03-281-0/+6
| | | | | | | Instead of loading all records and returning only a subset of those, just load the records as needed. Fixes #25537.
* Use `SET CONSTRAINTS` for `disable_referential_integrity` without superuser ↵Fumiaki MATSUSHIMA2017-03-261-1/+1
| | | | | | | | | | | | | | | privileges (take 2) Re-create https://github.com/rails/rails/pull/21233 eeac6151a5 was reverted (127509c071b4) because it breaks tests. ---------------- ref: 72c1557254 - We must use `authors` fixture with `author_addresses` because of its foreign key constraint. - Tests require PostgreSQL >= 9.4.2 because it had a bug about `ALTER CONSTRAINTS` and fixed in 9.4.2.
* Fix where_clause#except with specific where valueJohn Hawthorn2017-03-201-0/+8
| | | | | | | | | Fixes a regression introduced in 22ca710f20c3c656811df006cbf1f4dbc359f7a6 where Relation#unscope with a specific where value (vs unscoping the entire where clause) could result in the wrong binds being left on the query. This was caused by an index variable not being incremented properly.
* Use `load` rather than `collect` for force loadingRyuta Kamizono2017-03-191-7/+7
| | | | | | | Since b644964b `ActiveRecord::Relation` includes `Enumerable` so delegating `collect`, `all?`, and `include?` are also unneeded. `collect` without block returns `Enumerable` without preloading by that. We should use `load` rather than `collect` for force loading.
* Allow order to be given expressions as hash keysEugene Kenny2017-02-271-1/+24
| | | | | | | | | | | | | When `order` is given a hash, the keys are currently assumed to be attribute names and are quoted as such in the query, which makes it impossible to pass an expression instead: Post.order("LENGTH(title)" => :asc).last # SELECT `posts`.* FROM `posts` ORDER BY `posts`.`LENGTH(title)` DESC LIMIT 1 If the key is an `Arel::Nodes::SqlLiteral`, we now use it directly in the query. This provides a way to build a relation with a complex order clause that can still be reversed with `reverse_order` or `last`.
* Merge pull request #27122 from kamipo/fix_unscope_with_subqueryRafael França2017-02-131-0/+12
|\ | | | | Fix unscope with subquery
| * Fix unscope with subqueryRyuta Kamizono2016-12-051-0/+12
| | | | | | | | | | | | | | Currently cannot unscope subquery properly. This commit fixes the issue. Fixes #26323.
* | Revert "Merge pull request #21233 from ↵Rafael Mendonça França2017-01-031-1/+1
| | | | | | | | | | | | | | | | | | | | mtsmfm/disable-referential-integrity-without-superuser-privileges" This reverts commit eeac6151a55cb7d5f799e1ae33aa64a839cbc3aa, reversing changes made to 5c40239d3104543e70508360d27584a3e4dc5baf. Reason: Broke the isolated tests. https://travis-ci.org/rails/rails/builds/188721346
* | Merge pull request #27563 from kamipo/fix_typoRafael França2017-01-031-1/+1
|\ \ | | | | | | Fix typo s/permited/permitted/
| * | Fix typo s/permited/permitted/Ryuta Kamizono2017-01-041-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | ``` % git grep -n permited actionview/test/template/url_helper_test.rb:238: def test_button_to_with_permited_strong_params actionview/test/template/url_helper_test.rb:245: def test_button_to_with_unpermited_strong_params activerecord/test/cases/relations_test.rb:1620: def test_update_on_relation_passing_active_record_object_is_not_permited ```
* | | Merge pull request #21233 from ↵Rafael França2017-01-031-1/+1
|\ \ \ | |/ / |/| | | | | | | | mtsmfm/disable-referential-integrity-without-superuser-privileges Use `SET CONSTRAINTS` for `disable_referential_integrity` without superuser privileges
| * | Use `SET CONSTRAINTS` for `disable_referential_integrity` without superuser ↵Fumiaki MATSUSHIMA2016-12-031-1/+1
| |/ | | | | | | | | | | | | | | | | privileges ref: 72c1557254 - We must use `authors` fixture with `author_addresses` because of its foreign key constraint. - Tests require PostgreSQL >= 9.4.2 because it had a bug about `ALTER CONSTRAINTS` and fixed in 9.4.2.
* | Remove deprecation of using ActiveRecord::Base instance in .updateRafael Mendonça França2017-01-031-2/+2
| |
* | Remove deprecated `#uniq`, `#uniq!`, and `#uniq_value`Ryuta Kamizono2016-12-301-6/+0
| |
* | Remove deprecated conditions parameter from #delete_allRafael Mendonça França2016-12-291-6/+0
| |
* | Remove deprecated conditions parameter from `#destroy_all`Rafael Mendonça França2016-12-291-6/+0
| |
* | Remove deprecated support to passing a class as a value in a queryRafael Mendonça França2016-12-291-9/+0
| |
* | "Use assert_nil if expecting nil. This will fail in minitest 6."Akira Matsuda2016-12-251-8/+8
|/
* Respect new records for `CollectionProxy#uniq`Ryuta Kamizono2016-11-131-1/+0
| | | | | | | | | | | | | Currently if `CollectionProxy` has more than one new record, `CollectionProxy#uniq` result is incorrect. And `CollectionProxy#uniq` was aliased to `distinct` in a1bb6c8b06db. But the `uniq` method and the `SELECT DISTINCT` method are different methods. The doc in `CollectionProxy` is for the `SELECT DISTINCT` method, not for the `uniq` method. Therefore, reverting the alias in `CollectionProxy` to fix the inconsistency and to have the both methods.
* Add more rubocop rules about whitespacesRafael Mendonça França2016-10-291-21/+21
|
* Tweak a test so the queries matchMatthew Draper2016-10-081-1/+1
|
* improve error message when include assertions failMichael Grosser2016-09-161-4/+4
| | | | | | assert [1, 3].includes?(2) fails with unhelpful "Asserting failed" message assert_includes [1, 3], 2 fails with "Expected [1, 3] to include 2" which makes it easier to debug and more obvious what went wrong
* Merge pull request #25987 from aquajach/masterRafael Mendonça França2016-08-171-0/+9
|\ | | | | | | Fix does_not_support_reverse? to find sql functions with commas in nested brackets
| * have does_not_support_reverse? support sql functions with commas in nested ↵Jack2016-07-291-0/+9
| | | | | | | | brackets
* | Add three new rubocop rulesRafael Mendonça França2016-08-161-17/+17
| | | | | | | | | | | | | | | | Style/SpaceBeforeBlockBraces Style/SpaceInsideBlockBraces Style/SpaceInsideHashLiteralBraces Fix all violations in the repository.
* | Add `Style/EmptyLines` in `.rubocop.yml` and remove extra empty linesRyuta Kamizono2016-08-071-1/+0
| |
* | applies remaining conventions across the projectXavier Noria2016-08-061-1/+1
| |
* | remove redundant curlies from hash argumentsXavier Noria2016-08-061-2/+2
| |
* | modernizes hash syntax in activerecordXavier Noria2016-08-061-89/+89
| |
* | applies new string literal convention in activerecord/testXavier Noria2016-08-061-251/+251
|/ | | | | The current code base is not uniform. After some discussion, we have chosen to go with double quotes by default.
* Merge pull request #24203 from sferik/count_with_blockKasper Timm Hansen2016-05-161-0/+5
|\ | | | | Forward ActiveRecord::Relation#count to Enumerable#count if block given
| * Forward ActiveRecord::Relation#count to Enumerable#count if block givenErik Michaels-Ober2016-03-191-0/+5
| |
* | Allow the connection adapters to determine the order of bind paramsSean Griffin2016-05-061-0/+18
|/ | | | | | | | | | | | In 5.0 we use bind parameters for limit and offset, while in 4.2 we used the values directly. The code as it was written assumed that limit and offset worked as `LIMIT ? OFFSET ?`. Both Oracle and SQL Server have a different syntax, where the offset is stated before the limit. We delegate this behavior to the connection adapter so that these adapters are able to determine how the bind parameters are flattened based on what order their specification has the various clauses appear. Fixes #24775
* Merge pull request #23751 from chezou/add-test-case-order-by-fieldAndrew White2016-02-221-0/+6
|\ | | | | Add test cases about MySQL ORDER BY FIELD()
| * Add assertions order by field with empty dataMichiaki Ariga2016-02-181-0/+6
| | | | | | | | | | | | | | | | | | | | | | | | | | | | Add assertions to MySQL `ORDER BY FIELD()` with empty data. These tests examine to sanitize `ORDER BY FIELD()` with empty data appropriately. ```ruby Tag.order(['field(id, ?)', []]).to_sql # => SELECT "tags".* FROM "tags" ORDER BY field(id, NULL) Tag.order(['field(id, ?)', nil]).to_sql # => SELECT "tags".* FROM "tags" ORDER BY field(id, NULL) ```
* | Mutating the result of Relation#to_a should not affect the relationMatthew Draper2016-02-211-0/+10
|/ | | | | | Clarifying this separation and enforcing relation immutability is the culmination of the previous efforts to remove the mutator method delegations.
* Introduce ActiveRecord::IrreversibleOrderErrorBogdan Gusiev2016-01-271-0/+34
| | | | | Raises when #reverse_order can not process SQL order instead of making invalid SQL before this patch
* Merge pull request #22053 from Empact/first-loadedYves Senn2015-12-281-3/+26
|\ | | | | | | Fix #first(limit) to take advantage of #loaded? records if available
| * Fix `first(limit)` to take advantage of `loaded?` records if availableBen Woosley2015-12-241-3/+14
|/ | | | | | | | | | | | | | | | | I realized that `first(2)`, etc. was unnecessarily querying for the records when they were already preloaded. This was because `find_nth_with_limit` can not know which `@records` to return because it conflates the `offset` and `index` into a single variable, while the `@records` only needs the `index` itself to select the proper record. Because `find_nth` and `find_nth_with_limit` are public methods, I instead introduced a private method `find_nth_with_limit_and_offset` which is called internally and handles the `loaded?` checking. Once the `offset` argument is removed from `find_nth`, `find_nth_with_limit_and_offset` can be collapsed into `find_nth_with_limit`, with `offset` always equal to `offset_index`.
* Revert "Perform a more efficient query in `Relation#any?`"Sean Griffin2015-12-141-0/+7
| | | | | | | | | This reverts commit 6d5b1fdf55611de2a1071c37544933bb588ae88e. `eager_load` and `references` can include hashes, which won't match up with `references` A test case has been added to demonstrate the problem
* Merge pull request #13008 from ktheory/sanitize_orderSean Griffin2015-10-291-0/+5
|\ | | | | | | Support SQL sanitization in AR::QueryMethods#order
| * Support SQL sanitization in AR::QueryMethods#orderAaron Suggs2013-11-221-0/+5
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Add support for sanitizing arrays in SQL ORDER clauses. This is useful when using MySQL `ORDER BY FIELD()` to return records in a predetermined way. ```ruby Tag.order(['field(id, ?', [1,3,2]].to_sql # => SELECT "tags".* FROM "tags" ORDER BY field(id, 1,3,2) ``` Prior to this, developers must be careful to sanitize `#order` arguments themselves.
* | Add deprecation warning to `ActiveRecord::Relation#update`Ted Johansson2015-10-151-0/+7
| | | | | | | | | | | | | | | | When passing an instance of `ActiveRecord::Base` to `#update`, it would internally call `#find`, resulting in a misleading deprecation warning. This change gives this deprecated use of `#update` its own, meaningful warning.
* | Skip the test added in 9cc324a on buggy versions of SQliteSean Griffin2015-09-211-10/+0
| | | | | | | | See 7dcfc25e7c52682a4343c2ba7188a69e7c06c936 for more details
* | Ensure aliased attributes passed to `select` are quoted if using `from`Sean Griffin2015-09-211-0/+10
| | | | | | | | | | | | Fixes #21488 [Sean Griffin & johanlunds]
* | Deprecate passing conditions to AR::Relation destroy_all and delete_all methodsWojciech Wnętrzak2015-09-061-0/+12
| |
* | Fix regression caused by a01d164bRafael Mendonça França2015-07-071-0/+19
| | | | | | | | | | | | | | | | | | When preload is used in a default scope the preload_values were returning nested arrays and causing the preloader to fail because it doesn't know how to deal with nested arrays. So before calling preload! we need to splat the arguments. This is not needed to includes because it flatten its arguments.
* | Merge pull request #20196 from huoxito/preload-association-and-mergesRafael Mendonça França2015-05-281-1/+27
|\ \ | | | | | | Properly append preload / includes args on Merger
| * | Properly append preload / includes args on MergerWashington Luiz2015-05-281-1/+27
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Couldn't find other way to get the association name from a given class other than looping through `reflect_on_all_associations` reflections .. Noticed this one while looking at this example: ```ruby class Product < ActiveRecord::Base has_many :variants has_many :translations end class Translation < ActiveRecord::Base belongs_to :product end class Variant < ActiveRecord::Base belongs_to :product end class BugTest < Minitest::Test def test_merge_stuff product = Product.create! name: 'huhu' variant = Variant.create! product_id: product.id Translation.create! locale: 'en', product_id: product.id product_relation = Product.all .preload(:translations) .joins(:translations) .merge(Translation.where(locale: 'en')) .where(name: 'huhu') assert_equal variant, Variant.joins(:product).merge(product_relation).first end end ```
* | | deprecate `Relation#uniq` use `Relation#distinct` instead.Yves Senn2015-05-261-4/+7
|/ / | | | | | | | | | | | | | | | | See #9683 for the reasons we switched to `distinct`. Here is the discussion that triggered the actual deprecation #20198. `uniq`, `uniq!` and `uniq_value` are still around. They will be removed in the next minor release after Rails 5.