Handling specific database exceptions


When an exception comes from rails, it has a name, i.e:
“AssociationTypeMismatch”, so I can catch it like this:

rescue ActiveRecord::AssociationTypeMismatch => e

However, how can I catch and handle specific exceptions that come from
the database. “rescue Exception => e” is not good enough. I want to be
able to deal with them individually. for example, mysql throws a
duplicate entry key error, how do I catch that in rails?


I am trying to figure out if I need to use the database specific error
codes, but I can’t figure out how to access it from rails…