Reputation: 237
I have a database table users
. I am trying to query the database to fetch all users whose ages are greater than 18.
adult_users = User.where('age > 18')
The above query will not be executed, as per lazy loading feature, instead it will just create a Active Record Relation. The actual query will be executed only when you call adult_users.first
or if you do some other operations with it.
But in rails console, even if you hit User.where('age > 18')
, you can see the query executing.
How could this happen?
Upvotes: 3
Views: 1324
Reputation: 18474
In console it does execute the query while inspecting the ActiveRecord::Relation
If logging is enabled you can even see the User Load
entry.
If you type
adult_users = User.where('age > 18'); nil
then variable will not get inspected and no query run
Upvotes: 11