如何Rails的ActiveRecord的链QUOT;其中"条款没有多个查询? [英] How does Rails ActiveRecord chain "where" clauses without multiple queries?

查看:93
本文介绍了如何Rails的ActiveRecord的链QUOT;其中"条款没有多个查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个PHP开发人员学习Ruby on Rails的awesomne​​ss,我爱的ActiveRecord,我发现了一些非常有趣的,这是ActiveRecord的方法如何检测方法链的末端执行查询。

I'm a PHP developer learning the awesomness of Ruby on Rails, I'm loving ActiveRecord and i noticed something really interesting, Which is how ActiveRecord methods detect the end of method chain to execute the query.

@person = Person.where(name: 'Jason').where(age: 26)

# In my humble imagination I'd think that each where() executes a database query
# But in reality, it doesn't until the last method in the chain

这是如何工作的巫术?

How does this sorcery work?

推荐答案

其中,方法返回一个的ActiveRecord ::关联对象,并且通过本身此对象没有发出数据库查询。它的其中的使用这个对象重要的。

The where method returns an ActiveRecord::Relation object, and by itself this object does not issue a database query. It's where you use this object that matters.

在控制台中,你可能做这样的:

In the console, you're probably doing this:

@person = Person.where(name: "Jason")

然后的 blammo 的它发出数据库查询和返回这似乎是大家的数组名为杰森。耶,活动记录!

And then blammo it issues a database query and returns what appears to be an array of everyone named Jason. Yay, Active Record!

不过,你做这样的事情:

But then you do something like this:

@person = Person.where(name: "Jason").where(age: 26)

然后发出另一个查询,但一个人的人谁是所谓的贾森谁是26,但它只是发出的一个的查询,所以在哪儿其他查询去了?

And then that issues another query, but this one's for people who are called Jason who are 26. But it's only issuing one query, so where'd the other query go?

正如其他人所建议的,这种情况正在发生,因为其中,方法返回一个代理对象。它实际上并不执行一个查询,除非它的要求做返回的数据集。

As others have suggested, this is happening because the where method returns a proxy object. It doesn't actually perform a query and return a dataset unless it's asked to do that.

当您运行的什么的控制台,它要输出什么是你运行结果的检查版本。如果你把 1 在控制台并按下Enter键,你会得到 1 回来,因为 1.inspect 1 。魔法!同样适用于1。各种自用物品等的不具有检查方法定义,因此Ruby回落到了一个在对象这返回的东西的可怕的如<对象#23adbf42560>

When you run anything in the console, it's going to output the inspected version of the outcome of whatever it is you ran. If you put 1 in the console and hit enter, you'll get 1 back because 1.inspect is 1. Magic! Same goes for "1". A variety of other objets don't have an inspect method defined and so Ruby falls back to the one on Object which returns something ghastly like <Object#23adbf42560>.

每一个的ActiveRecord ::关联对象上定义的检查的方法,它可以使一个查询。当你写的查询您的控制台,IRB会叫检查从该查询和输出的东西几乎是人类可读的,像数组,你会看到的返回值。

Every single ActiveRecord::Relation object has the inspect method defined on it so that it causes a query. When you write the query in your console, IRB will call inspect on the return value from that query and output something almost human readable, like the Array that you'd see.

如果你只是发出这在一个标准的Ruby脚本,则没有查询将被执行,直到对象进行了检查(通过检查),或者通过使用<$进行迭代C $ C>每个,或有 to_a 方法调用它。

If you were just issuing this in a standard Ruby script, then no query would be executed until the object was inspected (via inspect) or was iterated through using each, or had the to_a method called on it.

在此之前的这三件事情之一发生,你可以链接尽可能多的在这里就可以了语句,你会喜欢的,然后当你的执行的叫检查 to_a 每个就可以了,那么它最终将执行该查询。

Up until one of those three things happen, you can chain as many where statements on it as you will like and then when you do call inspect, to_a or each on it, then it will finally execute that query.

这篇关于如何Rails的ActiveRecord的链QUOT;其中&QUOT;条款没有多个查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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