繁体   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