Seeing as I cannot yet post comments, here is the answer to eager loading:
The N+1 problem means that ActiveRecord will not get the posts, when you query for all your users using User.all
, therefore ActiveRecord will fire a new SQL query that selects the last post for every single user you iterate over. If you have 50 users, that will be one query for the fifty users and then fifty queries for each last post.
In case you have many users, the latency between the database and the Rails application will slow down the performance of the site and make users wait.
Eager loading allows you to fetch associated models to the model you are querying, so you can write:
User.includes(:posts).all.map { |u| u.posts.last }
ActiveRecord will first query for all the users, then query for all posts associated with all the users at once before iterating over the users, thereby avoiding doing a new query for each user. This is only two queries.
However, this also has it's drawbacks if your users have tons of posts, because you'll be fetching ALL posts of ALL users and not just the latest posts, thereby straining the database and generating a lot of undesired I/O.