簡體   English   中英

如何按嵌套屬性排序嵌套屬性

[英]how to order nested attribute by nested attribute

我有擁有個人資料的用戶。 我可以這樣顯示個人資料:

@profiles.each do |p|
  puts p.user.last_name + " " + p.user.first_name
end 

我不知道如何按last_name進行排序,因為它是一個嵌套屬性,不是直接在Profiles表中的一部分。 我的用戶可以正確地建模訂單,但是不能在我通過用戶個人資料調用用戶名時建模。 謝謝你的幫助。 我應該制作“ profiles_helper.rb”文件還是應該在視圖中訂購?

您可以使用group_byprofiles轉換為數組,然后可以使用sort_by方法基於last_name進行排序

profiles = @profiles.group_by{|p| p.user.last_name}
result = profiles.sort_by{|last_name, profiles| last_name}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM