I know I’m looping twice in the following code for no reason. How could I go the following loop of records in one step instead of first selecting them and then performing actions on it.
merchants = Merchant.all.select do |merchant|
merchant.url.present? && merchant.url != merchant.new_data.url
end
merchants.each do |merchant|
merchant.url = merchant.new_data.url
merchant.save!
end
I bet it’s something very simple — I’m new to Ruby so all this is new to me.
It’s conceptually ok to iterate over data on separate steps (this allows modularization and composability). Granted, this may be a bit inefficient when using strict structures (as opposed to lazy ones) like Ruby arrays, but no worries for small/medium arrays. Anyway, of course you can join the logic in one step, like this: