What is wrong with code like User.find (: all) .each?

I just read this requirement in the job list:

Aware of the pitfalls of code like: User.find(:all).each

I immediately realized that I was unqualified for this job, because in my life I do not understand what the problem is. It...

  • is the design connected? Should I store the database query in a variable and then iterate over it?
  • dangerous?
  • too verbose? Should I use User.all.eachinstead? (-1 word! W00t!)
  • just poorly worded? If this is preceded by a "user table has 3 million rows"?
+3
source share
5 answers

, "", , , - User.all.each, :

User.all.each do |u|
    next if !u.is_active
    ...
end

, Ruby , , .

+7

User.all . 3 , 3 . . SQL , , , " "

+5

? User.all.each? (-1 ! W00t!)

. , , Model.each, , .

? " 3 " ​​

, . .

, , 3- , , .

+2

, , , , , . , , :

User.find_in_batches do |group|
  group.each do |user|
    user.do_something
  end
end

1 10 .

+1

Well, if you want to sound reasonable, you answer: "There is no trap for this question, assuming that you want to change or display the values ​​from each of the user objects. :-)

0
source

Source: https://habr.com/ru/post/1707823/


All Articles