Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 4 equivalent of Rails 3 'Model.all'

In Rails 3 if I wanted to hit the db I'd take .all on the end of a query. This is useful for when I'm doing things like refreshing cache on writes (so reads always hit cache).

Now in Rails 4, Model.all returns an ActiveRecord::Relation object (ie doesn't hit the db). What is the best way to get it actually go to the db and return the records specified?

like image 922
Davd_R Avatar asked Aug 27 '13 03:08

Davd_R


1 Answers

Model.all.to_a in Rails 4 will give the same result as Modal.all in Rails 3.

Amend:

@Bigxiang, pointed out in his comment to try Model.all.load, this method for certain hits the database but still returns an ActiveRecordRelation. And basically to get a similar functionality as Model.all from "Rails 3", you can call to_a on the returned ActiveRecordRelation.

like image 110
vee Avatar answered Sep 30 '22 10:09

vee