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

前端 未结 8 1193
北恋
北恋 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:54

    You could also create a new 'sort_order' column on the assignment table, and add a default scope like

    default_scope { order('sort_order desc')}
    

    to your assignments model.

提交回复
热议问题