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 Acras
ricardo-WlbSPrj98RBeoWH0uzbU5w@public.gmane.org
Acras Desenvolvimento de Sistemas
+55+41-3232-6404
www.acras.net
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk
-~----------~----~----~----~------~----~------~--~---
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Ricardo Acras wrote:> 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? >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 -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.3 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFFRM+VMyx0fW1d8G0RAhjrAJ950H2NAPot7NGrQdrgdJvSLkXCsACfWL+N hfm1A+cUm8QvsxJKNTr9YYY=EySQ -----END PGP SIGNATURE----- --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
> 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), > > ZachHello, 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---