diff options
author | Yves Senn <yves.senn@gmail.com> | 2013-03-12 10:23:08 +0100 |
---|---|---|
committer | Yves Senn <yves.senn@gmail.com> | 2013-03-15 14:15:47 +0100 |
commit | a1bb6c8b06db83546179175b9b2dde7912c86f9b (patch) | |
tree | 136a3a72f5e612b847e953b2ea12bacd53336040 /guides | |
parent | bfee706b2e8c3f0144588fb034d41c8333bcd88e (diff) | |
download | rails-a1bb6c8b06db83546179175b9b2dde7912c86f9b.tar.gz rails-a1bb6c8b06db83546179175b9b2dde7912c86f9b.tar.bz2 rails-a1bb6c8b06db83546179175b9b2dde7912c86f9b.zip |
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`.
Diffstat (limited to 'guides')
-rw-r--r-- | guides/source/active_record_querying.md | 11 |
1 files changed, 6 insertions, 5 deletions
diff --git a/guides/source/active_record_querying.md b/guides/source/active_record_querying.md index 4a4f814917..7355f6816c 100644 --- a/guides/source/active_record_querying.md +++ b/guides/source/active_record_querying.md @@ -76,6 +76,7 @@ The methods are: * `reorder` * `reverse_order` * `select` +* `distinct` * `uniq` * `where` @@ -580,10 +581,10 @@ ActiveModel::MissingAttributeError: missing attribute: <attribute> Where `<attribute>` is the attribute you asked for. The `id` method will not raise the `ActiveRecord::MissingAttributeError`, so just be careful when working with associations because they need the `id` method to function properly. -If you would like to only grab a single record per unique value in a certain field, you can use `uniq`: +If you would like to only grab a single record per unique value in a certain field, you can use `distinct`: ```ruby -Client.select(:name).uniq +Client.select(:name).distinct ``` This would generate SQL like: @@ -595,10 +596,10 @@ SELECT DISTINCT name FROM clients You can also remove the uniqueness constraint: ```ruby -query = Client.select(:name).uniq +query = Client.select(:name).distinct # => Returns unique names -query.uniq(false) +query.distinct(false) # => Returns all names, even if there are duplicates ``` @@ -1438,7 +1439,7 @@ Client.where(active: true).pluck(:id) # SELECT id FROM clients WHERE active = 1 # => [1, 2, 3] -Client.uniq.pluck(:role) +Client.distinct.pluck(:role) # SELECT DISTINCT role FROM clients # => ['admin', 'member', 'guest'] |