I am using find_by_sql() method in my controller,how can I handle exceptions raised by executing a sql query using this method?
Asked
Active
Viewed 521 times
0
-
1How would you handle any exception in Ruby? – Chris Heald Jun 29 '13 at 00:27
1 Answers
0
Your question is a little vague but I try to provide some information.
Rails is just Ruby, so learning how to handle Exceptions in Ruby is the key here, for that I recommend
Once you are done with that, it frankly depends on your application, I would say that you shouldn't handle it in the controller and that a Class or (a little uglier) a Model should do it, as they should have the Buisness Logic.
As a side note, you should really consider if a Exception is the way to go, Ruby, as many dynamic languages makes really easy to check for values so you can avoid raising it.
Lastly, if you just want to know how you could do it:
begin
# find_by_sql
rescue ActiveRecord::RecordNotFound
# handle not found error
rescue ActiveRecord::ActiveRecordError
# handle other ActiveRecord errors
rescue # StandardError
# handle most other errors
rescue Exception
# handle everything else
end
or the rescue_from
method in the controller.
From this answer.
Hope that helps in some way.

Community
- 1
- 1

nicosantangelo
- 13,216
- 3
- 33
- 47