aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/calculations_test.rb
Commit message (Collapse)AuthorAgeFilesLines
* Remove deprecated `#uniq`, `#uniq!`, and `#uniq_value`Ryuta Kamizono2016-12-301-4/+0
|
* Add more rubocop rules about whitespacesRafael Mendonça França2016-10-291-6/+6
|
* 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
* Add three new rubocop rulesRafael Mendonça França2016-08-161-2/+2
| | | | | | | | Style/SpaceBeforeBlockBraces Style/SpaceInsideBlockBraces Style/SpaceInsideHashLiteralBraces Fix all violations in the repository.
* modernizes hash syntax in activerecordXavier Noria2016-08-061-18/+18
|
* applies new string literal convention in activerecord/testXavier Noria2016-08-061-88/+88
| | | | | The current code base is not uniform. After some discussion, we have chosen to go with double quotes by default.
* Fix Active Record test failureRyuta Kamizono2016-07-121-3/+2
| | | | Caused at a45363a2fb53e0a016f33dd211c00b5d81764379.
* Always prefer class types to query types when casting `group`Sean Griffin2016-07-111-0/+6
| | | | | | | | | | | | | When `group` is used in combination with any calculation method, the resulting hash uses the grouping expression as the key. Currently we're incorrectly always favoring the type reported by the query, instead of the type known by the class. This causes differing behavior depending on whether the adaptor actually gives proper types with the query or not. After this change, the behavior will be the same on all adaptors -- we see if we know the type from the class, fall back to the type from the query, and finally fall back to the identity type. Fixes #25595
* Forward ActiveRecord::Relation#count to Enumerable#count if block givenErik Michaels-Ober2016-03-191-0/+4
|
* remove args from assert_nothing_raised in testsTara Scherner de la Fuente2016-02-221-2/+2
|
* Remove legacy mysql adapterRyuta Kamizono2015-12-211-2/+2
| | | | Follow up to #22642.
* Remove legacy mysql adapterAbdelkader Boudih2015-12-171-1/+1
|
* Fix generated projection fields in group by queryRafael Sales2015-10-221-0/+5
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | Closes #21922 Let `Book(id, author_id)`, `Photo(id, book_id, author_id)` and `Author(id)` Running `Book.group(:author_id).joins(:photos).count` will produce: * Rails 4.2 - conflicts `author_id` in both projection and group by: ```sql SELECT COUNT(*) AS count_all, author_id AS author_id FROM "books" INNER JOIN "photos" ON "photos"."book_id" = "books"."id" GROUP BY author_id ``` * Master (9d02a25) - conflicts `author_id` only in projection: ```sql SELECT COUNT(*) AS count_all, author_id AS author_id FROM "books" INNER JOIN "photos" ON "photos"."book_id" = "books"."id" GROUP BY "books"."author_id" ``` * With this fix: ```sql SELECT COUNT(*) AS count_all, "books"."author_id" AS books_author_id FROM "books" INNER JOIN "photos" ON "photos"."book_id" = "books"."id" GROUP BY "books"."author_id" ```
* Merge pull request #20653 from repinel/allow-arel-select-countSean Griffin2015-10-201-0/+52
|\ | | | | Allow select using Arel and perform a count
| * Allow select with Arel and count as well as calculations with ArelRoque Pinel2015-06-301-0/+52
| | | | | | | | | | | | | | | | | | | | | | It allows a query like `User.select(:name).count` to be written using Arel as `User.select(User.arel_table[:name]).count`. It exposes the calculations API to accept Arel nodes: `User.count(User.arel_table[:name])`, `User.sum(User.arel_table[:id])`, `Account.average(Account.arel_table[:credit_limit])`, `Account.maximum(Account.arel_table[:credit_limit])` and `Account.minimum(Account.arel_table[:credit_limit])`.
* | Qualify column names in calculationSoutaro Matsumoto2015-10-201-0/+19
| | | | | | | | Column names inserted via `group` have to be qualified with table name.
* | Fix AC::Parameters not being sanitized for query methods.Guo Xiang Tan2015-10-021-0/+32
|/
* Use `Enumerable#sum` on `ActiveRecord::Relation` when a block is givenSean Griffin2015-06-191-0/+10
| | | | | | | | This matches our behavior in other cases where useful enumerable methods might have a different definition in `Relation`. Wanting to actually enumerate over the records in this case is completely reasonable, and wanting `.sum` is reasonable for the same reason it is on `Enumerable` in the first place.
* Fix postgresql DISTINCT requirement in pluck testKevin Deisz2015-06-021-2/+2
|
* Allow Enumerable#pluck to take a splat.Kevin Deisz2015-05-291-0/+21
| | | | | | This allows easier integration with ActiveRecord, such that AR#pluck will now use Enumerable#pluck if the relation is loaded, without needing to hit the database.
* deprecate `Relation#uniq` use `Relation#distinct` instead.Yves Senn2015-05-261-3/+6
| | | | | | | | | 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.
* Fix referencing wrong aliases while joining tables of has many throughpinglamb2015-03-221-0/+11
| | | | | | | | | | | association While joining table of has_many :through association, ActiveRecord will use the actual table name instead of through-join alias. It results with a wrong SQL and exception is raised. This only happens when calculation methods like #count is called. This issue is affecting Rails 4.1.x and 4.2.x as well.
* Allow a symbol to be passed to `attribute`, in place of a type objectSean Griffin2015-02-061-3/+3
| | | | | | | | | | | | | | | | | | The same is not true of `define_attribute`, which is meant to be the low level no-magic API that sits underneath. The differences between the two APIs are: - `attribute` - Lazy (the attribute will be defined after the schema has loaded) - Allows either a type object or a symbol - `define_attribute` - Runs immediately (might get trampled by schema loading) - Requires a type object This was the last blocker in terms of public interface requirements originally discussed for this feature back in May. All the implementation blockers have been cleared, so this feature is probably ready for release (pending one more look-over by me).
* Don't error when grouped calculations return 0 recordsSean Griffin2015-01-281-0/+5
| | | | Fixes #18717
* Add test case for joined pluckSean Griffin2014-12-111-0/+7
| | | | https://github.com/rails/rails/commit/39542fba54328ca048fb75a5d5b37f8e1d4c1f37#commitcomment-8938379
* Improve the test case introduced by bd0d47eSean Griffin2014-12-111-19/+10
|
* Fix ProtocolViolation/bind message supplies for polymorphic + pluck or groupMiklos Fazkeas2014-12-111-0/+24
|
* Don't require calculations to be aliased to a columnSean Griffin2014-10-311-1/+0
| | | | | | | Arel has changed so that `.sum` no longer aliases `SUM(the_column)` to `sum_id`. This means the type returned by the adapter will be at the key `"SUM(the_column)"`. Longer term, we should eventually be able to retain type information from the AR::Base subclasses used in joined queries
* Move association definition to the model fileAkira Matsuda2014-08-281-2/+0
|
* Be sure that test fixtures satisfy referential integrity before calculatingAkira Matsuda2014-08-281-1/+1
| | | | | | | There exists some other test files that load :minivans fixtures but don't load :speedometers. Loading :speedometers here prevents the following error when this test was run after such test: CalculationsTest#test_should_group_by_association_with_non_numeric_foreign_key: ActiveRecord::RecordNotFound: Couldn't find all Speedometers with 'speedometer_id': (ABC, s1) (found 1 results, but was looking for 2)
* Don't use `Column` for type casting in Relation calculationsSean Griffin2014-06-181-5/+0
|
* Pluck should work with columns of the same name from different tablesSean Griffin2014-06-111-0/+7
| | | | | | | | The column name given by the adapter doesn't include the table namespace, so going through the hashed version of the result set causes overridden keys. Fixes #15649
* Rename `property` to `attribute`Sean Griffin2014-06-071-3/+3
| | | | For consistency with https://github.com/rails/rails/pull/15557
* Deprecate decimal columns being automatically treated as integersSean Griffin2014-05-271-0/+4
| | | | | | With ActiveRecord::Properties, we now have a reasonable path for users to continue to keep this behavior if they want it. This is an edge case that has added a lot of complexity to the code base.
* Ignore order when doing count.Lauro Caetano2014-04-071-0/+14
| | | | | | | This is necessary because Postgresql doesn't play nice with ORDER BY and no GROUP BY. Fixes #14621.
* Ensure AR #second, #third, etc. finders work through associationsJason Meller2014-01-211-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This commit fixes two regressions introduced in cafe31a078 where newly created finder methods #second, #third, #forth, and #fifth caused a NoMethodError error on reload associations and where we were pulling the wrong element out of cached associations. Examples: some_book.authors.reload.second # Before # => NoMethodError: undefined method 'first' for nil:NilClass # After # => #<Author id: 2, name: "Sally Second", ...> some_book.first.authors.first some_book.first.authors.second # Before # => #<Author id: 1, name: "Freddy First", ...> # => #<Author id: 1, name: "Freddy First", ...> # After # => #<Author id: 1, name: "Freddy First", ...> # => #<Author id: 2, name: "Sally Second", ...> Fixes #13783.
* Ensure #second acts like #first AR finderJason Meller2014-01-201-8/+10
| | | | | | | | | | | | This commit bring the famous ordinal Array instance methods defined in ActiveSupport into ActiveRecord as fully-fledged finders. These finders ensure a default ascending order of the table's primary key, and utilize the OFFSET SQL verb to locate the user's desired record. If an offset is defined in the query, calling #second adds to the offset to get the actual desired record. Fixes #13743.
* Fix type cast on group sum with custom expressionPaul Nikitochkin2013-12-101-0/+4
| | | | | | | | For PG adapters with custom expression and grouped result of aggregate functions have not found correct column type for it. Extract column type from query result. Closes: #13230
* Change test_registering_new_handlers and test_count_on_invalid_columns_raisesYasuo Honda2013-08-021-1/+1
| | | | | tesetcases assertion to case insensitive because Oracle database adapter handles table name in uppercase.
* Remove deprecated `:distinct` option from `Relation#count`.Yves Senn2013-07-011-10/+0
|
* Remove fall back and column restrictions for `count`.Yves Senn2013-06-091-0/+9
|
* Merge pull request #10561 from Empact/nix-throwresultJon Leighton2013-06-071-0/+11
|\ | | | | Rather than raising ThrowResult when construct_limited_ids_conditions comes up empty, set the relation to NullRelation and rely on its results.
| * Add coverage for the fact that pluck without an argument returns all the ↵Ben Woosley2013-05-101-0/+5
| | | | | | | | table's columns.
| * Fix that #pluck wasn't rescuing ThrowResult, meaning it would blow up when ↵Ben Woosley2013-05-101-0/+6
| | | | | | | | failing to construct_limited_ids_condition.
* | Remove #sum with a block was deprecated.kennyj2013-06-011-6/+0
|/
* Handle aliased attributes in ActiveRecord::Relation.Godfrey Chan2013-05-011-0/+12
| | | | | | | | | | | | | | | | | | | | | | | | | | When using symbol keys, ActiveRecord will now translate aliased attribute names to the actual column name used in the database: With the model class Topic alias_attribute :heading, :title end The call Topic.where(heading: 'The First Topic') should yield the same result as Topic.where(title: 'The First Topic') This also applies to ActiveRecord::Relation::Calculations calls such as `Model.sum(:aliased)` and `Model.pluck(:aliased)`. This will not work with SQL fragment strings like `Model.sum('DISTINCT aliased')`. Github #7839 *Godfrey Chan*
* replace #merge with relation API calls in calculations_test.Yves Senn2013-04-021-17/+13
|
* Deprecate the `:distinct` option for `Relation#count`.Yves Senn2013-03-151-4/+14
| | | | | | | We moved more and more away from passing options to finder / calculation methods. The `:distinct` option in `#count` was one of the remaining places. Since we can now combine `Relation#distinct` with `Relation#count` the option is no longer necessary and can be deprecated.
* rename `Relation#uniq` to `Relation#distinct`. `#uniq` still works.Yves Senn2013-03-151-1/+2
| | | | | | | | The similarity of `Relation#uniq` to `Array#uniq` is confusing. Since our Relation API is close to SQL terms I renamed `#uniq` to `#distinct`. There is no deprecation. `#uniq` and `#uniq!` are aliases and will continue to work. I also updated the documentation to promote the use of `#distinct`.
* `#count` in conjunction with `#uniq` performs distinct count.Yves Senn2013-01-261-0/+4
| | | | closes #6865