查找ActiveRecord的对象由子对象的最大字段值? [英] Find ActiveRecord object by maximum field value of a child object?

查看:146
本文介绍了查找ActiveRecord的对象由子对象的最大字段值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何能找到一个ActiveRecord计算的结果,而不是一个值相关联的对象?

例如我已经@parent其中有许多儿童。我想找到孩子具有最大的价值。

我知道我可以做@ parent.children.maximum(:值),但它返回的最大值。是否有返回值的全部对象,而不是让我可以用不同的领域从最大的对象相似,最大和最小的方法?

解决方案

  @ parent.children.order(值DESC)。第一
 

How can I find the object associated with the results of an ActiveRecord Calculation rather than a value?

For example I have @parent which has many children. I would like to find the child with the maximum 'value'.

I understand that I can do @parent.children.maximum(:value), but this returns the maximum value. Is there a method similar to maximum and minimum that returns the entire object instead of the value so that I can use different fields from the maximum object?

解决方案

@parent.children.order("value DESC").first

这篇关于查找ActiveRecord的对象由子对象的最大字段值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆