问题描述
我是一名 PHP 开发人员,正在学习 Ruby on Rails 的强大功能,我喜欢 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?
推荐答案
where
方法返回一个 ActiveRecord::Relation
对象,并且该对象本身不会发出一个数据库查询.重要的是你在哪里使用这个对象.
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它发出一个数据库查询并返回一个看起来是一个名为 Jason 的每个人的数组.是的,Active Record!
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 岁的名为 Jason 的人的.但它只发出一个查询,那么另一个查询去哪里了?
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?
正如其他人所建议的,发生这种情况是因为 where
方法返回一个代理对象.它实际上不会执行查询并返回数据集,除非被要求这样做.
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.
当您在控制台中运行 anything 时,它将输出您运行的任何结果的检查版本.如果你在控制台输入 1
并回车,你会得到 1
因为 1.inspect
是 1
>.魔法!"1"
也是如此.许多其他对象没有定义 inspect
方法,因此 Ruby 退回到 Object
上的方法,它返回一些可怕的,例如 .
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 objects 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::Relation
对象都定义了 inspect
方法,以便引发查询.当您在控制台中编写查询时,IRB 将对该查询的返回值调用 inspect
并输出一些几乎人类可读的内容,例如您将看到的 Array.
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 脚本中发出此命令,则在检查对象(通过 inspect
)或通过使用 each
进行迭代之前,不会执行任何查询,或者调用了 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.
在这三件事之一发生之前,您可以根据需要在其上链接任意数量的 where
语句,然后在您做时调用 inspect
, to_a
或 each
,然后它会最终执行该查询.
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 如何链接“where"没有多个查询的子句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!