本文介绍了在活动记录中返回自定义查询选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个查询,它会做一些数学运算并返回一个带有结果集的计算出的自定义选择字段.我无法弄清楚如何在返回的 activerecord 对象中访问它.我也为它添加了一个 attr_accessor.
I've got a query that does some math and returns a calculated custom select field with the result set. I cannot figure out how to access that in the activerecord object that is returned. I've added an attr_accessor for it also.
attr_accessor :percentage_used
select('gateways.*, (num_transactions_today/ SUM(num_transactions_today)) AS percentage_used ').joins(:gateway_groups).where('map_gateway_groups.gateway_group_id = ?', gateway_group_id)
在结果集中,我希望能够访问 :percentage_used,但它不在那里.关于我做错了什么的任何想法?我以前从不需要这样做.
in the result set, I would expect to have access to :percentage_used, but it is not in there. Any ideas on what i'm doing wrong? i've never needed to do this before.
谢谢
推荐答案
您可以访问它作为
object["percentage_used"]
这篇关于在活动记录中返回自定义查询选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!