Avoid N+1?

I don’t think you can do it in one query, but I’m not a DB expert. You can remove the N+1 query, though:

recent_posts = Post.recent.page(params[:page])
liked_posts = Post.joins(:likes).where(id: posts.ids, "likes.user_id" => current_user.id)

recent_posts.map! do |recent_post|
  if liked_posts.include? recent_post
    serialize(recent_post).merge({is_liked: true})
  else
    serialize(recent_post).merge({is_liked: false})
  end
end

Basically you get two sets and have to run through them. Don’t know how this works in terms of performance, but it should do the job with only two queries.

I’m not sure how to serialize a post, but in order to merge that has to be an hash, if you have an attr_accessor for liked in the model, then you can just set that. Also it is common practice to used liked= and liked? for boolean fields, instead of is_liked.