How do I order a has_many through association in Ruby on Rails?

前端 未结 8 1293
北恋
北恋 2020-12-15 02:45

Given the following AR models, I would like to sort users alphabetically by last name when given a handle to a task:

#user
has_many :assignments
has_many :ta         


        
8条回答
  •  一个人的身影
    2020-12-15 02:50

    I am using Rails (5.0.0.1) and could make the sort with this syntax in my model Group that has many users through group_users:

    # Associations.
    has_many :group_users
    has_many :users, -> { order(:name) }, through: :group_users
    

    Adjust the code to your need that will work.

提交回复
热议问题