I realize that this could be a rare occurrence (that two or more users would have the same blog post title) but this is something my client wants so, I have to figure it out.
I have a query @blog_posts (which is a query on Posts that changes based on location, etc). I need a way to to list out all the posts titles and how many times that title occurs within the query @blog_posts
Like this:
How to clean a car (2)
I love baseball (1)
Is there a standard practice for grouping and sorting?
In summary, I need to count the occurrences in the query @blog_posts = Post.where(...)
(for example) -- not all posts in existence.
Yes, you can do it with a named scope inside your model (rails 3+ syntax):
In your controller:
Something like this?
Of course you need to modify the query so that your conditions etc. are included.
Sounds like you basically want to count occurrences: this answer could be adapted to your purposes.
If you're on 1.8.7 or newer, consider
group_by
: