通过ActiveRecord的访问SQL计算列 [英] Access SQL computed columns through ActiveRecord

查看:86
本文介绍了通过ActiveRecord的访问SQL计算列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型,其中包括一个属性重新presenting出生数据(birth_date <$ C C $> )。

I have a Person model, which includes a property representing the data of birth (birth_date).

我也有一个名为年龄()方法,能够统计出的人的目前的年龄。

I also have a method called age(), which works out the current age of the person.

我现在需要运行基于人的年龄查询,所以我复制的逻辑年龄()在MySQL中的计算列。

I now have need to run queries based on the person's age, so I have replicated the logic of age() as a computed column in MySQL.

我不能锻炼我怎么会做模型的默认select语句的这一附加列的部分。

I cannot workout how I would make this additional column part of the default select statement of the model.

我希望能够访问岁时就好像它是在人模型的原生属性,执行查询人反对,访问的价值在我的意见。

I would like to be able to access the age as if it were a native property of the Person model, to perform queries against it and access the value in my views.

这是可能的,还是我找错了树?

Is this possible, or am barking up the wrong tree?

我想我也许可以定义附加字段通过 default_scope 范围,但这些方法似乎只承认现有油田。我也试过 default_scope 亦随 attr_assessor ​​

I thought I might be able to define additional fields through default_scope or scope, but these methods seem to only recognise existing fields. I also tried default_scope in tandem with attr_assessor.

可能的解决方法,我认为,但会preFER不要做:

Possible workarounds I've considered but would prefer not to do:

  • 创建一个名为实际属性的年龄和填充通过使用回调。日期总是在变化,所以这显然是可靠的。

  • Create an actual property called age and populate through the use of callbacks. The date is always changing, so this obviously would be be reliable.

在复制ActiveRecord的逻辑为年龄(),并在范围为那里的原因。这将实现我所需要的,但并不觉得很干。

Replicate the logic in ActiveRecord as age() and in a scope as a where cause. This would achieve what I need, but doesn't feel very DRY.

我已经缓存年龄()方法的结果。它是用在我最感兴趣的是where子句中的字段的能力。

I am already caching the results of the age() method. it is the ability to use the field in where clauses that I am most interested in.

必须有一种方法来定义通过SQL动态场,我可以在默认情况下,通过该模型进行访问。

There must be a way to define dynamic fields through SQL that I can access through the model by default.

任何帮助将是AP preciated。

Any help would be appreciated.

更新

我的失败尝试利用领域的一个例子:

An example of my failed attempt to utilise scopes:

default_scope :select => "*, 2 as age"

attr_accessor :age

年龄是空白,我以为是因为示波器只处理限制,不能延长。

age is blank, I assume because scopes only deal with limiting, not extending.

推荐答案

kim3er你解决你的问题很简单。请按照下列步骤操作:

kim3er your solution to your problem is simple. Follow these steps:

松开 attr_accessor:年龄从你的模型。你根本不需要它。

Loose the attr_accessor :age from your model. You simply don't need it.

发表于模型的默认范围: default_scope:选择=&GT; *,2年龄

Leave the default scope at the Person model: default_scope :select => "*, 2 as age"

最后打开一个控制台,并尝试

Lastly open up a console and try

p = Person.first
p.age
=> 2

当您使用定义为,一个选择的Rails 将自动地添加对实例的方法为您服务!所以回答你的问题:

When you define a select using as, Rails will automagically add those methods on the instances for you! So the answer to your question:

必须有一种方法来定义通过SQL动态场,我可以在默认情况下,通过该模型进行访问。

There must be a way to define dynamic fields through SQL that I can access through the model by default.

是:

Rails的

这篇关于通过ActiveRecord的访问SQL计算列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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