在活动记录中返回自定义查询选择
我有一个查询,它会做一些数学运算并返回一个带有结果集的计算出的自定义选择字段.我无法弄清楚如何在返回的 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"]
相关文章