How to access translation key for ActiveModel validation error?

activemodel, ruby-on-rails, ruby-on-rails-3

Solution

Okay I finally got it!

The solution is NOT to patch or do anything on the rails side of things - instead - the answer lies in the `I18n` gem.

I18n, which rails uses for translations by standard, has the ability to plugin new backends to provide more flexibility to it. In this case, the backend called metadata does exactly what I needed. When adding `I18n::Backend::Simple.include(I18n::Backend::Metadata)` to an initializer, it gives me the possibility of extracting all translation related information directly from the error message string by adding the method call `translation_metadata`.

A fantastic simple solution to a complicated problem :-)

Problem

I Have a situation, where I wan't to store the translation key for a validation error in my db instead of the error message it self. Imagine the following situation: ``` class Car < ActiveRecord::Base validates_presence_of :year, :fuel end car = Car.new(:fuel => 'Diesel') car.save! #=> ActiveRecord::RecordInvalid ``` Now if I call: ``` car.errors #=> :year=>["can't be blank"] ``` I get the translated error message. Instead I would like to extract the translation key that would generate this (in this case I think it would be something like `errors.messages.blank`), so I can store it in my database in a different model e.g. `FailedCar` so I can later on generate an I18n customized form for filling in the missing information manually in a view. UPDATE I think it's this method that I need to hook into. I want to fetch the key and the options returned, so I can perform the translations again at a later point in time.

Original source