Artwork

Content provided by Charles Max Wood. All podcast content including episodes, graphics, and podcast descriptions are uploaded and provided directly by Charles Max Wood or their podcast platform partner. If you believe someone is using your copyrighted work without your permission, you can follow the process outlined here https://player.fm/legal.
Player FM - Podcast App
Go offline with the Player FM app!

025 RC Eager Loading

7:28
 
Share
 

Archived series ("Inactive feed" status)

When? This feed was archived on July 14, 2021 01:41 (3y ago). Last successful fetch was on June 05, 2018 17:42 (6y ago)

Why? Inactive feed status. Our servers were unable to retrieve a valid podcast feed for a sustained period.

What now? You might be able to find a more up-to-date version using the search function. This series will no longer be checked for updates. If you believe this to be in error, please check if the publisher's feed link below is valid and contact support to request the feed be restored or if you have any other concerns about this.

Manage episode 150840406 series 1008985
Content provided by Charles Max Wood. All podcast content including episodes, graphics, and podcast descriptions are uploaded and provided directly by Charles Max Wood or their podcast platform partner. If you believe someone is using your copyrighted work without your permission, you can follow the process outlined here https://player.fm/legal.
Eager loading is a terrific way of speeding up your response times. Let's consider a piece of code that pulls a list of associated objects from the database. For example, a view that displays the posts written by a given user: <% @user.posts.each do |post| %> <%= render post %> <% end %> (I know that partial has a collection option for things like this. I feel that this code sample is more expressive for our discussion.) In this case, assuming that @user was loaded by calling User.find(1), your Rails application is going to go to the database for each post. So, if the user has 40 posts in the system, the Rails application will make 40 trips to the database to get the posts. The problem is that if there is any latency on the database connection, it adds up. The fix for this is the use of the 'includes' method when querying for the user. User#includes tells the query to pull in whatever associated records you specify in its arguments. So, if we found @user by calling User.includes([:posts]).find(1), it would pull the user's record and all of the user's posts records and cache all of the user's posts on the user's object. The argument for 'includes' is an array. The objects in the array can be symbols or hashes. Symbols represent that associations the Model has. Hashes are used for including nested associations. For example, if we wanted to pull all of the comments on the user's posts. Here's another example: # In the controller @user = User.includes([:address, :phone_number, {:posts => [:comments]}]).find(1) # In the view <% @user.posts.each do |post| %> <%= render post %> <% post.comments.each do |comment| %> <%= render comment %> <% end %> <% end %>
  continue reading

33 episodes

Artwork

025 RC Eager Loading

Rails Coach

published

iconShare
 

Archived series ("Inactive feed" status)

When? This feed was archived on July 14, 2021 01:41 (3y ago). Last successful fetch was on June 05, 2018 17:42 (6y ago)

Why? Inactive feed status. Our servers were unable to retrieve a valid podcast feed for a sustained period.

What now? You might be able to find a more up-to-date version using the search function. This series will no longer be checked for updates. If you believe this to be in error, please check if the publisher's feed link below is valid and contact support to request the feed be restored or if you have any other concerns about this.

Manage episode 150840406 series 1008985
Content provided by Charles Max Wood. All podcast content including episodes, graphics, and podcast descriptions are uploaded and provided directly by Charles Max Wood or their podcast platform partner. If you believe someone is using your copyrighted work without your permission, you can follow the process outlined here https://player.fm/legal.
Eager loading is a terrific way of speeding up your response times. Let's consider a piece of code that pulls a list of associated objects from the database. For example, a view that displays the posts written by a given user: <% @user.posts.each do |post| %> <%= render post %> <% end %> (I know that partial has a collection option for things like this. I feel that this code sample is more expressive for our discussion.) In this case, assuming that @user was loaded by calling User.find(1), your Rails application is going to go to the database for each post. So, if the user has 40 posts in the system, the Rails application will make 40 trips to the database to get the posts. The problem is that if there is any latency on the database connection, it adds up. The fix for this is the use of the 'includes' method when querying for the user. User#includes tells the query to pull in whatever associated records you specify in its arguments. So, if we found @user by calling User.includes([:posts]).find(1), it would pull the user's record and all of the user's posts records and cache all of the user's posts on the user's object. The argument for 'includes' is an array. The objects in the array can be symbols or hashes. Symbols represent that associations the Model has. Hashes are used for including nested associations. For example, if we wanted to pull all of the comments on the user's posts. Here's another example: # In the controller @user = User.includes([:address, :phone_number, {:posts => [:comments]}]).find(1) # In the view <% @user.posts.each do |post| %> <%= render post %> <% post.comments.each do |comment| %> <%= render comment %> <% end %> <% end %>
  continue reading

33 episodes

All episodes

×
 
Loading …

Welcome to Player FM!

Player FM is scanning the web for high-quality podcasts for you to enjoy right now. It's the best podcast app and works on Android, iPhone, and the web. Signup to sync subscriptions across devices.

 

Quick Reference Guide