Created
September 22, 2009 17:50
-
-
Save danhixon/191263 to your computer and use it in GitHub Desktop.
undefined method self_and_descendants_from_active_record
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=begin | |
Upgraded to rails 2.3.4 and got the following error: | |
undefined method self_and_descendants_from_active_record for Class:Object | |
My class doesn't descend from ActiveRecord but I was using ActiveRecord::Errors for validations | |
attr_accessor :errors | |
def initialize | |
#this hack is to get an instance of ActiveRecord::Errors: | |
self.errors = ActiveRecord::Errors.new(Object.new) | |
end | |
And I overerride the valid? method. | |
But now I get the undefined method error. | |
This page had the work around but the html encoding made it unusable: | |
http://puretec.wordpress.com/2009/09/09/activerecorderrors-newself-and-undefined-method-self_and_descendants_from_active_record/#comment-17 | |
Below is the code I added to my class to restore the previous functionality. | |
=end | |
def self.self_and_descendants_from_active_record | |
[self] | |
end | |
def human_attribute_name(attribute_key_name, options = {}) | |
defaults = self_and_descendants_from_active_record.map do |klass| | |
"#{klass.name.underscore}.#{attribute_key_name}""#{klass.name.underscore}.#{attribute_key_name}" | |
end | |
defaults << options[:default] if options[:default] | |
defaults.flatten! | |
defaults << attribute_key_name.humanize | |
options[:count] ||= 1 | |
I18n.translate(defaults.shift, options.merge(:default => defaults, :scope => [:activerecord, :attributes])) | |
end | |
def human_name(options = {}) | |
defaults = self_and_descendants_from_active_record.map do |klass| | |
"#{klass.name.underscore}""#{klass.name.underscore}" | |
end | |
defaults << self.name.humanize | |
I18n.translate(defaults.shift, {:scope => [:activerecord, :models], :count => 1, :default => defaults}.merge(options)) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment