I'm trying to write an ActiveRecord find query with no success. Please help!
This feature of my project is kind of a blog with Digg-like features: each registered user can add a star to the blog post itself or any of its replies.
So here are the relevant model classes:
- BlogPost (has_many Replies)
- Reply (belongs_to Blog Post)
- Starring (this represents the action of one users starring one BlogPost or Reply; it has a polymorphic interface to BlogPost and Reply, referring to them internally as :starrables, replete with :starrable_id and :starrable_type fields)
So, what I am trying to do is write a method in blog_post.rb that will return the total number of starrings for the BlogPost object as well as all of its Reply classes. That is, if a blog post has 10 stars, and it has two replies which each have 5 stars, it should return 20.
How do I write this query without making a jillion database transactions?