I have a Post model that has_many :comments. The question is: how can I build a query using ActiveRecord Query Interface that retrieves all posts and the last comment of each posts?
All I have right now is the following, but it doesn’t filter the comments and I have no idea of what to do next:
Post.includes(:comments)
Well, the simplest thing comes to mind, which may not be the best is creating a scope.
And then use
Be sure you have indexes in your SQL.