h1

ActiveRecord::Errors.new(self) and undefined method self_and_descendants_from_active_record

September 9, 2009

We recently switched to rails 2.3.4 and noticed the following error:

undefined method self_and_descendants_from_active_record for Cart:Class

Our Cart class has no ActiveRecord binding, but we need some validations, so we simple use

@errors = ActiveRecord::Errors.new(self)

and overerride the valid? action.

But now we get the undefined method error.

Solution

Implement in your class also:

 def self.self_and_descendants_from_active_record
    [self]
  end

  def self.human_name(options = {})
    defaults = self_and_descendants_from_active_record.map do |klass|
      :"#{klass.name.underscore}"
    end
    defaults < [:activerecord, :models], :count => 1, :default => defaults}.merge(options))
  end

  def self.human_attribute_name(attribute_key_name, options = {})
    defaults = self_and_descendants_from_active_record.map do |klass|
      :"#{klass.name.underscore}.#{attribute_key_name}"
    end
    defaults << options[:default] if options[:default]
    defaults.flatten!
    defaults < defaults, :scope => [:activerecord, :attributes]))
  end

and you can still use ActiveModel like validations 🙂

Advertisements

Kommentar verfassen

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

WordPress.com-Logo

Du kommentierst mit Deinem WordPress.com-Konto. Abmelden / Ändern )

Twitter-Bild

Du kommentierst mit Deinem Twitter-Konto. Abmelden / Ändern )

Facebook-Foto

Du kommentierst mit Deinem Facebook-Konto. Abmelden / Ändern )

Google+ Foto

Du kommentierst mit Deinem Google+-Konto. Abmelden / Ändern )

Verbinde mit %s

%d Bloggern gefällt das: