Add method to Activerecord::Base

Asked

Viewed 73 times

1

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

Method of Activerecord::Base

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

In the model

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

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

1 answer

2


Test like this:

return uuid unless self.class.method_defined? :md5_fields
  • It worked, thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.