I''m generating 3 models on 1 controller and i''m rescuing ActiveRecord::RecordInvalid for validation errors, however 2 of the models have the same attribute called "name", and if there''s an error on that field, I wouldn''t know whether that belongs to model A or model B. What then is the best way of modifying the error message of a validation error without having to rely on locales which I think ActiveRecord encourages you to do. Currently, my solution is overriding the RecordInvalid#initialize (adding "model class: #{record.class}" to the :errors hash value), but I dont think this is a clean solution. module ActiveRecord class RecordInvalid def initialize(record) @record = record errors = @record.errors.full_messages.join( I18n.t(''support.array.words_connector'', :default => '', '') ) super(I18n.t(''activerecord.errors.messages.record_invalid'', :errors => "model class: #{record.class}: #{errors}")) end end end -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Reginald Tan wrote in post #1019523:> I''m generating 3 models on 1 controller and i''m rescuing > ActiveRecord::RecordInvalid for validation errors,What are you doing in the rescue block? Why can''t you do something like this: rescue ActiveRecord::RecordInvalid => e puts e.record.class end> however 2 of the > models have the same attribute called "name", and if there''s an error on > that field, I wouldn''t know whether that belongs to model A or model B. > > What then is the best way of modifying the error message of a validation > error without having to rely on locales which I think ActiveRecord > encourages you to do. > > Currently, my solution is overriding the RecordInvalid#initialize > (adding "model class: #{record.class}" to the :errors hash value), but I > dont think this is a clean solution. > > > module ActiveRecord > class RecordInvalid > def initialize(record) > @record = record > errors = @record.errors.full_messages.join( > I18n.t(''support.array.words_connector'', :default => '', '') > ) > super(I18n.t(''activerecord.errors.messages.record_invalid'', > :errors => "model class: #{record.class}: #{errors}")) > end > end > end-- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
You''re right. How come I didn''t think of that. Thanks for the tip :D -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.