在Rails 3.2.6中延迟加载

时间:2022-06-13 11:00:17

I have found in several resources online than when doing stuff like:

我在网上找到了一些资源,而不是像:

cars = Car.where(:colour => 'black')

The query is not executed, until you do something like:

查询不会执行,除非您执行以下操作:

cars.each {|c| puts c.name } 

However, in my Rails 3.2.6 project, when I do the following in the console:

然而,在我的Rails 3.2.6项目中,当我在控制台中执行以下操作时:

User.where(:first_name => "John")

I get the following:

我得到以下:

 User Load (1.2ms)  SELECT `users`.* FROM `users` WHERE `users`.`first_name` = 'John'

So, the query is being executed right?

那么,查询正在执行,对吗?

Where did the lazy loading go? Or am I missing something here?

懒惰的装载到哪里去了?还是我漏掉了什么?

3 个解决方案

#1


34  

The console calls inspect on the result of any expression you type so that it can display it to you. inspect is one of the things that will trigger the load of the query. If you instead do

控制台调用inspect来查看您输入的任何表达式的结果,以便它可以显示给您。inspect是将触发查询的加载的内容之一。如果你不是做

x = User.where(:first_name => 'John'); false

then you should see no query because this time the console is calling inspect on false instead of on the Active Record relation object.

然后您应该看不到查询,因为这一次控制台调用的是false而不是活动记录关系对象。

#2


3  

This is an interesting question....The answer is that when executing something in IRB/console, it calls inspect on the resulting object and then prints it out. If you did something like:

这是一个有趣的问题....答案是,当在IRB/console中执行某些内容时,它会调用生成对象的inspect,然后将其打印出来。如果你做了这样的事情:

User.where(:first_name => "John").class

you should get back an ActiveRecord::Relation object.

您应该返回一个ActiveRecord:关系对象。

So the lazy loading for Rails still holds, it's just the way the console works.

所以Rails的延迟加载仍然有效,这只是控制台的工作方式。

Hope this helps.

希望这个有帮助。

Source(s): 1) https://rails.lighthouseapp.com/projects/8994/tickets/4951-rails-console-executes-where-queries-without-lazy-loading 2) Why Active Record relation is not returned in console?

1) https://rails.lighseapp.com/projects/8994/tickets/4951-rails-consoells-executes -where- queris-withoutlazy-loading 2)为什么在控制台中不返回活动记录关系?

#3


-1  

I ran a test using sqllite3 to attempt to find out if the AR Base find actually did a query immediately. Here is what I did:

我使用sqllite3运行了一个测试,试图找出AR Base find是否立即执行了查询。我所做的是:

rows=Customer.orders.find(1,2)

Then I did:

然后我做了:

ActiveRecord::Base.remove_connection;
p rows

I got a connection not established error.

我得到了一个连接而不是建立错误。

I also tried "p rows" immediately after the query w/o removing the connection and got the 2 rows I expected.

我还在查询w/o删除连接后立即尝试了“p行”,得到了我所期望的两行。

This was done with activerecord-3.1.3. My conclusion is that in 3.1.3 base find waits to do the query until the array (Relation?) is accessed.

这是用activerecord-3.1.3实现的。我的结论是在3.1.3 base find中等待执行查询,直到访问数组(关系?)。

I'm new at Ruby so my test might not be designed correctly.

我是Ruby新手,所以我的测试可能设计得不正确。

#1


34  

The console calls inspect on the result of any expression you type so that it can display it to you. inspect is one of the things that will trigger the load of the query. If you instead do

控制台调用inspect来查看您输入的任何表达式的结果,以便它可以显示给您。inspect是将触发查询的加载的内容之一。如果你不是做

x = User.where(:first_name => 'John'); false

then you should see no query because this time the console is calling inspect on false instead of on the Active Record relation object.

然后您应该看不到查询,因为这一次控制台调用的是false而不是活动记录关系对象。

#2


3  

This is an interesting question....The answer is that when executing something in IRB/console, it calls inspect on the resulting object and then prints it out. If you did something like:

这是一个有趣的问题....答案是,当在IRB/console中执行某些内容时,它会调用生成对象的inspect,然后将其打印出来。如果你做了这样的事情:

User.where(:first_name => "John").class

you should get back an ActiveRecord::Relation object.

您应该返回一个ActiveRecord:关系对象。

So the lazy loading for Rails still holds, it's just the way the console works.

所以Rails的延迟加载仍然有效,这只是控制台的工作方式。

Hope this helps.

希望这个有帮助。

Source(s): 1) https://rails.lighthouseapp.com/projects/8994/tickets/4951-rails-console-executes-where-queries-without-lazy-loading 2) Why Active Record relation is not returned in console?

1) https://rails.lighseapp.com/projects/8994/tickets/4951-rails-consoells-executes -where- queris-withoutlazy-loading 2)为什么在控制台中不返回活动记录关系?

#3


-1  

I ran a test using sqllite3 to attempt to find out if the AR Base find actually did a query immediately. Here is what I did:

我使用sqllite3运行了一个测试,试图找出AR Base find是否立即执行了查询。我所做的是:

rows=Customer.orders.find(1,2)

Then I did:

然后我做了:

ActiveRecord::Base.remove_connection;
p rows

I got a connection not established error.

我得到了一个连接而不是建立错误。

I also tried "p rows" immediately after the query w/o removing the connection and got the 2 rows I expected.

我还在查询w/o删除连接后立即尝试了“p行”,得到了我所期望的两行。

This was done with activerecord-3.1.3. My conclusion is that in 3.1.3 base find waits to do the query until the array (Relation?) is accessed.

这是用activerecord-3.1.3实现的。我的结论是在3.1.3 base find中等待执行查询,直到访问数组(关系?)。

I'm new at Ruby so my test might not be designed correctly.

我是Ruby新手,所以我的测试可能设计得不正确。