Home > Net >  Is there any advantage to using find method and includes method together?
Is there any advantage to using find method and includes method together?

Time:09-03

It does not seem to make sense to use includes and find together as follows

User.includes(:friends).find(1)

The number of records to be retrieved is only one, and if user.friends is called, the number of query calls will be the same as without includes.

Is there any advantage to using find method and includes method together?

Best regards.

CodePudding user response:

There is no benefit to using includes with find. Two queries(1 for finding the object and 1 for loading the association) will be fired in either case. The below code fires the same queries as your code:

user = User.find(1)
user.friends    # wherever you need to use the associated friends objects.
  • Related