Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use order in find_each rails

I really do not know whether someone has already asked this question or not, if someone has already asked this, please let me know.

I want to use order with find_each. It should something like

Email.find_each(:order=> "id desc").do |email|
  puts "email.id"
end

Thanks in advance

like image 884
railsman Avatar asked Mar 03 '14 11:03

railsman


1 Answers

Since find_each internally uses find_in_batches, which forces order to be ascending on the primary key (id ASC by default) and doesn't allow to change it, it's not possible.

More info here:

http://api.rubyonrails.org/classes/ActiveRecord/Batches.html

like image 113
Marek Lipka Avatar answered Nov 15 '22 01:11

Marek Lipka