Hey All,
Can someone explain the difference between these two constructs?
Consider a Projects model, which has_many :tasks–so tasks here is (I
believe) an array.
Construct #1:
tasks.reject(&:new_record?).each do |task|
# do some stuff with task
end
Construct #2:
tasks.each do |task|
unless task.new_record?
# do some stuff with task
end
end
I had thought 2 was just a more verbose version of 1, but it seems
that’s not the case. I grabbed #1 from here:
#75 Complex Forms Part 3 - RailsCasts (the existing_task_attributes=
method). I used construct #2 in my app, and found that not every task
was getting processed.
(Specifically, if the user removed every task on the view and then
saved, all but one would be deleted.)
Reading the one line of documentation for Array#reject, I don’t
understand why these 2 constructs should behave differently.
Many thanks!
-Roy