Reputation: 9617
There is something I don't fully understand. Look at this example which makes n calls (or queries) to database where n is number of customers.
@reports = Report.where(:car => car)
for customer in customers
report = @reports.where(:city => customer.city, :age=> customer.age)
end
This one below just call the database once, right?
@reports = Report.where(:car => car).where( :city => customers.map(&:city), :age => customers.map(&:age))
for customer in customers
report = @reports.detect(:city => customer.city, :age=> customer.age)
end
Upvotes: 0
Views: 279
Reputation: 7742
Yes, the second example just makes on call to the database.
When you do @reports= Report.where(:car => car).where( :city => customers.map(&:city), :age => customers.map(&:age))
you are loading all those records into the @reports
variable.
So at the detect part it doesn't need to call the database, since the records are already there.
Upvotes: 1