authors_with_welcoming_post_titles=Author.all.merge!(:joins=>:posts,:where=>"posts.title like 'Welcome%'").calculate(:count,'authors.id',:distinct=>true)
authors_with_welcoming_post_titles=Author.all.merge!(joins: :posts,where: "posts.title like 'Welcome%'").distinct.calculate(:count,'authors.id')
assert_equalreal_count,authors_with_welcoming_post_titles,"inner join and conditions should have only returned authors posting titles starting with 'Welcome'"
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