Using Miniprofiler, I see that my blog index page is calling an SQL query for every blog post in order to find the number of votes. How can I eager load the votes so that I can consolidate these queries?
The SQL I see is something like the following for each entry.
SELECT "rs_reputations".* FROM "rs_reputations" WHERE "rs_reputations"."reputation_name" = 'votes' AND "rs_reputations"."target_id" = 8 AND "rs_reputations"."target_type" = 'Post' LIMIT 1
In my posts_controller, I am currently eager loading the writer (Writer has_many :posts).
@posts = Post.includes(:writer).paginate(page: params[:page]), :per_page => 10)
How can I add :votes to this? I have tried
@posts = Post.includes(:writer, :rs_reputation).paginate(page: params[:page]), :per_page => 10)
and
@posts = Post.includes(:writer, :votes).paginate(page: params[:page]), :per_page => 10)
neither of which work.
My Post model is as below
post.rb
belongs_to: :writer
has_reputation :votes, source: :writer, aggregated_by: sum