根据相关模型中的值对模型记录进行排序
Sort model records based on values in related model
我目前在成员模型文件中对成员记录进行排序:
default_scope -> { order(:organization_id, :username) }
组织和成员之间存在 1:many 关系。我不想根据 organization_id 进行排序,而是根据组织名称对成员进行排序,这是组织模型中的一列。我应该怎么做?
我尝试了以下几行,但这行不通;它似乎只接受来自同一模型的变量,而不接受与其有关系的模型的变量。
default_scope -> { order(:organization.name, :username) }
default_scope -> { order(organization.name, :username) }
使用.includes
default_scope -> { includes(:organization).order('organizations.name , username') }
使用这行代码:
default_scope -> { includes(:organizations).order('organizations.name , username') }
我目前在成员模型文件中对成员记录进行排序:
default_scope -> { order(:organization_id, :username) }
组织和成员之间存在 1:many 关系。我不想根据 organization_id 进行排序,而是根据组织名称对成员进行排序,这是组织模型中的一列。我应该怎么做?
我尝试了以下几行,但这行不通;它似乎只接受来自同一模型的变量,而不接受与其有关系的模型的变量。
default_scope -> { order(:organization.name, :username) }
default_scope -> { order(organization.name, :username) }
使用.includes
default_scope -> { includes(:organization).order('organizations.name , username') }
使用这行代码:
default_scope -> { includes(:organizations).order('organizations.name , username') }