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完成吗?
答案 0 :(得分:0)
您似乎可以在模型中执行此操作:
has_many :people, ->{ order('first_name') }, through: :memberships
这在某些情况下有效,但我不接受它作为答案,因为我真正需要的是在控制器中设置它的方法,以便可以动态控制顺序。