Rails includes is a method that can greatly reduce the number of accesses when retrieving data from multiple related tables.
ruby.rb
@events = @events.includes(:member)
If you give the name of the association to the includes method, it will be associated immediately after the actual query. Issued a query to get model objects in bulk. In the above example: it will issue a query to get the member objects in bulk.
Looking at the issued sql ...
SELECT "staff_members".*FROM "staff_members"
WHERE "staff_members"."id" IN (1,2,3,4,5)
Such sql is issued. In other words, use the where clause instead of selecting one by one. A convenient method that solves the N + 1 problem by issuing SQL in a batch.
Recommended Posts