Rails 4 equivalent of Rails 3 'Model.all'

匿名 (未验证) 提交于 2019-12-03 03:10:03

问题:

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?

回答1:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!