I have a Rails app and am in the process of refactoring my controllers and looking at all the DB queries made by my application.
Would like to use include/join as much as possible to limit the number of DB queries made and I'm using devise for authentication.
Here's the problem I'm having:
Currently, let's say that the current user (as determined by Devise) has many Services and has many Friends. If, I want to display the user, services, and friends, I can do something like this in my controller:
def show
@friends = @current_user.friends
@services = @current_user.services
end
Now the problem here is that I will have made three individual SQL queries. One to grab the current user (which Devise handles), another to get the friends and a third one to get the services.
Instead (if it weren't for Devise handling the SQL query on the user), I could use the include method to do something like:
current_user = User.where(<devise's method to find the user>).include(:services).include(:friends)
To get the exact object that I need for each controller endpoint with only one query to the database.
Any ideas on how to do this? Do I have to start modifying Devise's current_user method or is there a better way to do this?