Extending activeRecord::Base

Hello,

I want to add some behaviour to ActiveRecord::Base, so I did this:

1) Added ActiveRecordExtensions.rb in lib directory
2) Added the following code to this file

module ActiveRecordExtended

class ActiveRecord::Base
   def date_formatter_for(field_name)
     module_eval(
       "def solicitation_date_formatted \n" +
  ...
end
end

3) Added the following line to environment.rb

  require 'ActiveRecordExtensions'

4) In every model that I want to use the extension I add the following line

    include ActiveRecordExtended

It works well, I have all the functions available in my model. But,
validations just stop working. all validates_presence_of just don't
work. Anybody knows what could be this?

Thanks in advance.

Ricardo,

  It really depends what your code is in your module. Could you post
that to the list? Also, ActiveRecord::Extensions is already an existing
plugin, which adds plugin architecture to ActiveRecord. You may not want
to use that name (or maybe you do),

Zach

Hello,

I guess I found a solution. My code now is like this:

module ActiveRecord
   def Base.date_formatter_for(field_name)
     module_eval(
       "def solicitation_date_formatted \n" +
       " solicitation_date.strftime '%d/%m/%Y' if solicitation_date
!= nil \n" +
       "end \n\n"
     )

     module_eval(
       "def solicitation_date_formatted=(value) \n " +
       "self.solicitation_date = Date.strptime(value,'%d/%m/%Y') \n " +
       "end \n "
     )
   end
end

Notice that I deleted the line

class ActiveRecord::Base

from the declaration. I think I, in some way, passed through the
inheritances of the class by redeclaring it. Declaring just the
methods works fine.

Thank you