Rails 4: find all records

后端 未结 4 1893
庸人自扰
庸人自扰 2021-01-31 14:02

Now that ActiveRecord::Relation#all is deprecated in Rails 4, how are you supposed to iterate all records?

Used to be like:

Foo.all.each do |foo|
  # w         


        
4条回答
  •  佛祖请我去吃肉
    2021-01-31 15:06

    This appears to be an incorrect deprecation warning somewhere in Rails. As of Rails 4.0.2 the warning message still exists. I get the following error when I try and run Foo.all:

    DEPRECATION WARNING: Relation#all is deprecated. If you want to eager-load a relation, you can call #load (e.g. Post.where(published: true).load). If you want to get an array of records from a relation, you can call #to_a (e.g. Post.where(published: true).to_a).

    I'm almost 100% certain I watched in a RailsCasts that #all was being changed to return an Relation in Rails 4 (instead of an array) - no mention of deprecation.

提交回复
热议问题