Rails active record single error patch


/ Published in: Ruby
Save to your folder(s)



Copy this code and paste it in your HTML
  1. # config/environment.rb
  2. require "#{RAILS_ROOT}/app/overrides/all"
  3.  
  4. # app/overrides/all.rb
  5. Dir[ File.dirname( __FILE__ ) + "/**/*.rb" ].each { |file| require( file ) }
  6.  
  7. # app/overrides/active_record.rb
  8. module ActiveRecord
  9. class Errors
  10. # ONLY RETURN THE 1ST ERROR FOR EACH ATTRIBUTE
  11. def first_messages
  12. list = []
  13. first_messages = []
  14. @errors.each_key do |attr|
  15. @errors[ attr ].each do |msg|
  16. next if msg.nil?
  17. if attr == 'base'
  18. unless list.include? attr
  19. first_messages << msg
  20. list << attr
  21. end
  22. else
  23. unless list.include? attr
  24. first_messages << @base.class.human_attribute_name( attr ) + ' ' + msg
  25. list << attr
  26. end
  27. end
  28. end
  29. end
  30. return first_messages
  31. end
  32. end
  33. end

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.