I am trying to optimize my code with eager loading, but when ever where function is called, a query is executed in logs.
@votes_list = Vote.joins(:user => :profile).where(:post_id => post.id)
@male_votes = @votes_list.where(:profiles => { :gender => 1 }).count
@female_votes = @votes_list.where(:profiles => { :gender => 2 }).count
I am trying to make few queries after the first one, without need to fetch from database, how to do it?
You want to eagerly load the Users and their Profile for each vote. Then you can select the sub-set of votes in-memory broken down by gender on the profile.