完成
rails 4. sqlite3 db(用于开发)。
我的模型看起来像这样:
class Group < ActiveRecord::Base
has_many :memberships
has_many :people, through: :memberships
has_many :notes, as: :notable
scope :with_active_members, -> { joins(people: [:role]).where(people: {active: true}).uniq }
end
我这样使用它:
@groups = Group.with_active_members.order("groups.position")
连接效果很好。但是我想按字母顺序订购人民记录。非常清楚,我不想想命令他们的相关人物订购。我希望这些小组可以按position
字段订购,但我希望按字母顺序订购相关的人群。可以使用ORM?
看来您可以在模型中执行此操作:
has_many :people, ->{ order('first_name') }, through: :memberships
这在某些情况下起作用,但是我不接受它作为答案,因为我真正需要的是在控制器中设置此问题的一种方法,因此可以动态地控制订单。