Lazy loading in Rails 3.2.6

前端 未结 3 1668
遥遥无期
遥遥无期 2021-02-07 07:13

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

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

The query is not executed, until

3条回答
  •  情话喂你
    2021-02-07 07:42

    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:

    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.

    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.

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

提交回复
热议问题