How to join an indirect association in ActiveRecord query in Ruby on Rails?
问题 In my Ruby on Rails application, I have a model Instance which belongs to another model Zone . The Zone model itself belongs to Country model. I am fetching a set of Instance objects as follows: scope :thisweek, -> { joins(:zone).where(zones: {created_at: ...}).includes(:zone) I would like to join Country to Zone and Instance as well, and then sort the result Instance set based on the zone.country.name field. Anyone can help me please? 回答1: You can try the following: scope :this_week, proc do