返回定制的ActiveRecord的查询中选择 [英] return custom query select in activerecord

查看:122
本文介绍了返回定制的ActiveRecord的查询中选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,做一些数学并返回一个计算的自定义选择字段的结果集的查询。我无法弄清楚如何访问,在返回的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"]

这篇关于返回定制的ActiveRecord的查询中选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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