rails: `references` does not work with nested `includes`

The following code works fine in Rails 4.0 and 4.1:

Movie.first.schedules.includes(:show).references(:show)

but this works only in Rails 4.0, and is broken in 4.1

Movie.first.schedules.includes(show: [:language]).references(:show)

In Rails 4.1, when the a nested include is used, the SQL generated seems to rename the shows table into shows_schedules for some reason, while continuing to refer to it later as simply shows. And this is with or without references being used.

LEFT OUTER JOIN `shows` `shows_schedules` ON `shows_schedules`.`id` = `schedules`.`show_id` 
..... 
INNER JOIN `shows` ON `schedules`.`show_id` = `shows`.`id`

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 21 (14 by maintainers)

Most upvoted comments

Thanks for figuring it out, for the explanation, and the workaround 😄

So the argument passed to references helps it decide which of my included tables I want to force a JOIN on? So do I need to pass in the pluralized table name then? I assume this would be the theoretically correct usage:

 Movie.first.schedules.latest
              .includes(show: [:language]).includes(theatre: [:city])
              .where('cities.id = ?', 42).references(:cities)