Gaurav Saini
Gaurav Saini

Reputation: 137

rails 3 where statement

In rails 3, the where statement of active record returns an active record object. i.e it uses lazy loading like

cars = Car.where(:colour => 'black') # No Query
cars.each {|c| puts c.name } # Fires "select * from cars where ..."

but when I fires,

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

in console, it returns the result without this lazy loading why ?

Upvotes: 0

Views: 245

Answers (1)

crishoj
crishoj

Reputation: 5927

Your console implicitly calls inspect on the result of your expression, which triggers the query.

You can avoid the inspection by appending a semicolon:

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

Upvotes: 1

Related Questions