Commit Graph

141 Commits

Author SHA1 Message Date
Michael Grosser
a9aed2ac94
improve error message when include assertions fail
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
2016-09-16 12:03:37 -07:00
Rafael Mendonça França
55f9b8129a
Add three new rubocop rules
Style/SpaceBeforeBlockBraces
Style/SpaceInsideBlockBraces
Style/SpaceInsideHashLiteralBraces

Fix all violations in the repository.
2016-08-16 04:30:11 -03:00
Xavier Noria
d22e522179 modernizes hash syntax in activerecord 2016-08-06 19:37:57 +02:00
Xavier Noria
9617db2078 applies new string literal convention in activerecord/test
The current code base is not uniform. After some discussion,
we have chosen to go with double quotes by default.
2016-08-06 18:26:53 +02:00
Ryuta Kamizono
b10b7917e6 Fix Active Record test failure
Caused at a45363a2fb53e0a016f33dd211c00b5d81764379.
2016-07-12 01:43:37 +09:00
Sean Griffin
a45363a2fb Always prefer class types to query types when casting group
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
2016-07-11 10:57:37 -04:00
Erik Michaels-Ober
58772397e9 Forward ActiveRecord::Relation#count to Enumerable#count if block given 2016-03-19 15:29:07 -07:00
Tara Scherner de la Fuente
926a24a751 remove args from assert_nothing_raised in tests 2016-02-22 22:56:23 -08:00
Ryuta Kamizono
af9878d640 Remove legacy mysql adapter
Follow up to #22642.
2015-12-21 08:46:55 +09:00
Abdelkader Boudih
fb24d0ed6c Remove legacy mysql adapter 2015-12-17 15:54:57 +00:00
Rafael Sales
c2d33c4abf Fix generated projection fields in group by query
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"
```
2015-10-22 04:22:57 -03:00
Sean Griffin
286c1c3aac Merge pull request #20653 from repinel/allow-arel-select-count
Allow select using Arel and perform a count
2015-10-20 16:17:14 -06:00
Soutaro Matsumoto
a7628099de Qualify column names in calculation
Column names inserted via `group` have to be qualified with table name.
2015-10-20 14:47:55 -06:00
Guo Xiang Tan
7d0b1e4847 Fix AC::Parameters not being sanitized for query methods. 2015-10-02 16:26:16 +08:00
Roque Pinel
b220c9f9b4 Allow select with Arel and count as well as calculations with Arel
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])`.
2015-06-30 17:36:51 -05:00
Sean Griffin
7d14bd3ff5 Use Enumerable#sum on ActiveRecord::Relation when a block is given
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.
2015-06-19 10:44:43 -06:00
Kevin Deisz
2dd95a775c Fix postgresql DISTINCT requirement in pluck test 2015-06-02 05:36:24 -04:00
Kevin Deisz
777fa257aa Allow Enumerable#pluck to take a splat.
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.
2015-05-29 10:32:32 -04:00
Yves Senn
adfab2dcf4 deprecate Relation#uniq use Relation#distinct instead.
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.
2015-05-26 10:35:14 +02:00
pinglamb
ba057a5ebb Fix referencing wrong aliases while joining tables of has many through
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.
2015-03-22 16:25:23 +08:00
Sean Griffin
101c19f55f Allow a symbol to be passed to attribute, in place of a type object
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).
2015-02-06 11:51:13 -07:00
Sean Griffin
74c2961bd8 Don't error when grouped calculations return 0 records
Fixes #18717
2015-01-28 16:01:12 -07:00
Sean Griffin
6cb956592c Add test case for joined pluck
39542fba54 (commitcomment-8938379)
2014-12-11 14:54:03 -07:00
Sean Griffin
39542fba54 Improve the test case introduced by bd0d47e 2014-12-11 13:34:46 -07:00
Miklos Fazkeas
bd0d47eed6 Fix ProtocolViolation/bind message supplies for polymorphic + pluck or group 2014-12-11 00:48:06 +01:00
Sean Griffin
53ec0bc055 Don't require calculations to be aliased to a column
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
2014-10-31 08:40:16 -06:00
Akira Matsuda
4304eb8b5e Move association definition to the model file 2014-08-28 15:50:14 +09:00
Akira Matsuda
9489b212eb Be sure that test fixtures satisfy referential integrity before calculating
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)
2014-08-28 15:30:29 +09:00
Sean Griffin
36bd52b482 Don't use Column for type casting in Relation calculations 2014-06-18 05:52:12 -06:00
Sean Griffin
fef044cd2b Pluck should work with columns of the same name from different tables
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
2014-06-11 15:12:36 -06:00
Sean Griffin
3fab9d8821 Rename property to attribute
For consistency with https://github.com/rails/rails/pull/15557
2014-06-07 07:20:25 -06:00
Sean Griffin
6b46106d65 Deprecate decimal columns being automatically treated as integers
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.
2014-05-27 05:42:45 -07:00
Lauro Caetano
bbad7523f0 Ignore order when doing count.
This is necessary because Postgresql doesn't play nice with ORDER BY and
no GROUP BY.

Fixes #14621.
2014-04-07 11:15:30 -03:00
Jason Meller
03855e790d Ensure AR #second, #third, etc. finders work through associations
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.
2014-01-21 19:35:27 -05:00
Jason Meller
cafe31a078 Ensure #second acts like #first AR finder
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.
2014-01-20 16:58:18 -05:00
Paul Nikitochkin
2a7fe7ae9b Fix type cast on group sum with custom expression
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
2013-12-10 14:30:12 +02:00
Yasuo Honda
d613034a11 Change test_registering_new_handlers and test_count_on_invalid_columns_raises
tesetcases assertion to case insensitive because Oracle database adapter
handles table name in uppercase.
2013-08-02 05:55:31 +09:00
Yves Senn
2181832fca Remove deprecated :distinct option from Relation#count. 2013-07-01 22:11:20 +02:00
Yves Senn
da9b5d4a84 Remove fall back and column restrictions for count. 2013-06-09 12:53:04 +02:00
Jon Leighton
a68c6cccf0 Merge pull request #10561 from Empact/nix-throwresult
Rather than raising ThrowResult when construct_limited_ids_conditions comes up empty, set the relation to NullRelation and rely on its results.
2013-06-07 03:03:18 -07:00
kennyj
5d75579eec Remove #sum with a block was deprecated. 2013-06-01 23:22:12 +09:00
Ben Woosley
48783ee7fc Add coverage for the fact that pluck without an argument returns all the table's columns. 2013-05-10 19:33:34 +02:00
Ben Woosley
2fcafee250 Fix that #pluck wasn't rescuing ThrowResult, meaning it would blow up when failing to construct_limited_ids_condition. 2013-05-10 19:27:12 +02:00
Godfrey Chan
54122067ac Handle aliased attributes in ActiveRecord::Relation.
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*
2013-05-01 16:36:01 -07:00
Yves Senn
be736265f0 replace #merge with relation API calls in calculations_test. 2013-04-02 16:20:35 +02:00
Yves Senn
cd87c85ef0 Deprecate the :distinct option for Relation#count.
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.
2013-03-15 14:15:47 +01:00
Yves Senn
a1bb6c8b06 rename Relation#uniq to Relation#distinct. #uniq still works.
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`.
2013-03-15 14:15:47 +01:00
Yves Senn
648def4060 #count in conjunction with #uniq performs distinct count.
closes #6865
2013-01-26 12:16:28 +01:00
Aaron Patterson
54a65183e7 fix PG typecasting errors 2012-12-28 19:11:02 -08:00
Carlos Antonio da Silva
ad9983f625 Deprecate Relation#sum with a block.
To perform a sum calculation over the array of elements, use to_a.sum(&block).

Please check the discussion in f9cb645dfcb5cc89f59d2f8b58a019486c828c73
for more context.
2012-11-21 22:21:01 -02:00