Reputation: 15141
I have a rake task like this:
task :update_all => :environment do
codes = get_all_codes
codes.each{ |code| find_or_create_from_my_data(code) }
end
Sometimes the update fails, so I want to know with which code failed. For that I wrote like this:
task :update_all => :environment do
begin
codes = get_all_codes
@code
codes.each{ |code| @code = code; find_or_create_from_my_data(code) }
rescue
p @code
end
end
It works fine, but I think it's a bit redundant. How can I write more effectively?
Upvotes: 2
Views: 4367
Reputation: 577
How about this:
task :update_all => :environment do
get_all_codes.each do |code|
begin
find_or_create_from_my_data(code)
rescue
p code
end
end
end
This way, even if one code fails, it will print it out and move on to the other ones instead of aborting early.
Upvotes: 1
Reputation: 961
the e.message will display for you which code failed and why
task :update_all => :environment do
codes = get_all_codes
codes.each{ |code| find_or_create_from_my_data(code) }
rescue => e
puts "(#{e.message})"
end
Upvotes: 2