Rails 4: find all records

后端 未结 4 1965
庸人自扰
庸人自扰 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 14:59

    Release notes for Rails 4:

    Model.all now returns an ActiveRecord::Relation, rather than an array of records. Use Relation#to_a if you really want an array.

    So your code will look like this:

    Foo.all.to_a.each do |foo|
      # whatever
    end
    

    See http://guides.rubyonrails.org/4_0_release_notes.html#active-record

提交回复
热议问题