I have a Post model that has an associated Comment model. Each comment has a user_id associated with a User model…all very standard stuff.
Essentially, when I fo a find query on all the Posts I’m interested in, I am looking for a way to efficiently get the number of unique user_id’s that have commented on each Post.
Have you used
counterCache? That will probably get you where you need to be. You will also want to combine that with some fancy conditions oncounterScope. This will allow you to save the number of commenters when a record is saved/updated, instead of on the fly; resulting in a faster page and a better user experience.Now, this alone may not get you to the final solution. Consider using a combination of counterCache/counterScope and virtualFields to pull the data and update that way.
Read about counterCache in the Book