Advanced ActiveRecord joins with index hints

It’s not entirely Rails-y (or ActiveRecord-ey?), but you can do:

Person.joins("INNER JOIN `memberships` USE INDEX(index_something_or_other) ON `memberships`.`person_id` = `people`.`id`")
      .joins("INNER JOIN `groups` ON `groups`.`id` = `memberships`.`group_id`")
      .where(groups: { name: 'dave' })
1 Like