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?
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With