Rails/Arel: Selecting all records as an ActiveRecord::Relation

浪子不回头ぞ 提交于 2019-11-30 02:48:47

For Rails 4.1 and above: Model.all returns a relation (where it previously did not)

For Rails 4.0: Model.where(nil)

For Rails 3.x: Model.scoped

Try relation = Model.scoped. That will give you the relation instead of the actual results.

You would want:

relation = Model.scoped

which if you see what relation is, it is in fact an ActiveRecord::Relation.

As you can see from this page:

http://api.rubyonrails.org/classes/ActiveRecord/NamedScope/ClassMethods.html#method-i-scoped

It says the following:

Anonymous scopes tend to be useful when procedurally generating complex queries, where passing intermediate values (scopes) around as first-class objects is convenient.

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