2016-11-25 56 views
0

Rails 4. Sqlite3 DB(用於開發)。如何在Rails中控制連接記錄的順序4

我的模型看起來是這樣的:

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

看來,你可以在模型中做到這一點:

has_many :people, ->{ order('first_name') }, through: :memberships 

這工作在某些情況下,但我不接受它的答案,因爲我真正需要的是設置這樣的方式控制器,以便可以動態控制訂單。