In my app, posts has many tags.
the tags are connected through a join table, join_tags
In my index view, which lists all the posts, I do something like so:
<% post.tags.each do |tag| %>
<%= tag.name %>,
<% end %>
The problem here, is its hitting the database for each post to load the tags.
Is there a way to load all of the tags for these tasks once in the controller? Maybe through the @Posts var? I have a feeling its through eager loading?
Yes you can, and as you said, eager loading is the right way to achieve this, you may want to do something like this in your controller action:
Assuming you have the following relationships in your post model:
It will save you the n+1 post-tag queries.