Add method in ActiveRecord :: Base

1

I created a method in ActiveRecord :: Base and it uses another method to be defined in the model, I need to validate if the model method is set before executing what I need, but it says is always undefined, what could it be? Ex:

ActiveRecord :: Base method

class ActiveRecord::Base
  def to_md5
    uuid = ''
    return uuid unless Module.method_defined? :md5_fields
    md5_fields.each do |mf|
      uuid += mf if mf
    end
    Digest::MD5.hexdigest(uuid)
  end
end

No model

 def md5_fields
   [cep, numero, complemento, cidade]
 end

When the model method is set it returns the MD5 , otherwise an empty string.

    
asked by anonymous 09.04.2015 / 16:16

1 answer

2

Test like this:

return uuid unless self.class.method_defined? :md5_fields
    
09.04.2015 / 16:21