apramanik
2008-Apr-04 10:31 UTC
Proper way to replace ActiveRecord methods for all Models.
Hi all,
What is the proper way to replace/wrap functionality in ActiveRecord
for all my models? Currently, I am doing this which works:
module ActiveRecord
module AttributeMethods
alias write_attribute_internal write_attribute
def write_attribute( attr_name, value )
value = nil if value == '''' # Empty strings are
considered nil!
write_attribute_internal( attr_name, value )
end
end
end
But it feels messy. Inheritance seems to be out of the question
because of the way ActiveRecord infers the table name (table_name()).
--~--~---------~--~----~------------~-------~--~----~
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?hl=en
-~----------~----~----~----~------~----~------~--~---
Rick DeNatale
2008-Apr-04 11:57 UTC
Re: Proper way to replace ActiveRecord methods for all Models.
On Fri, Apr 4, 2008 at 6:31 AM, apramanik <abhikp-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hi all, > > What is the proper way to replace/wrap functionality in ActiveRecord > for all my models? Currently, I am doing this which works: > > module ActiveRecord > > module AttributeMethods > > alias write_attribute_internal write_attribute > > def write_attribute( attr_name, value ) > > value = nil if value == '''' # Empty strings are considered nil! > > write_attribute_internal( attr_name, value ) > > end > > end > > end > > But it feels messy. Inheritance seems to be out of the question > because of the way ActiveRecord infers the table name (table_name()).Leaving aside the question of why you''d want to do this globally, such stuff is normally done in Rails using alias_method chain. Which would make this more conventional code: module ActiveRecord::AttributeMethods def write_attribute_with_empty_string_nilling( attr_name, value ) value = nil if value == '''' # Empty strings are considered nil! write_attribute_without_empty_string_nilling attr_name, value end alias_method_chain :write_attribute :empty_string_nilling end http://weblog.rubyonrails.org/2006/4/26/new-in-rails-module-alias_method_chain http://errtheblog.com/posts/48-aliasmethodbling -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/ --~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---
apramanik
2008-Apr-04 18:48 UTC
Re: Proper way to replace ActiveRecord methods for all Models.
Haha, it was just a test to see if it would work. Thanks for the info! On Apr 4, 4:57 am, "Rick DeNatale" <rick.denat...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Fri, Apr 4, 2008 at 6:31 AM, apramanik <abh...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hi all, > > > What is the proper way to replace/wrap functionality in ActiveRecord > > for all my models? Currently, I am doing this which works: > > > module ActiveRecord > > > module AttributeMethods > > > alias write_attribute_internal write_attribute > > > def write_attribute( attr_name, value ) > > > value = nil if value == '''' # Empty strings are considered nil! > > > write_attribute_internal( attr_name, value ) > > > end > > > end > > > end > > > But it feels messy. Inheritance seems to be out of the question > > because of the way ActiveRecord infers the table name (table_name()). > > Leaving aside the question of why you''d want to do this globally, such > stuff is normally done in Rails using alias_method chain. Which would > make this more conventional code: > > module ActiveRecord::AttributeMethods > def write_attribute_with_empty_string_nilling( attr_name, value ) > value = nil if value == '''' # Empty strings are considered nil! > write_attribute_without_empty_string_nilling attr_name, value > end > > alias_method_chain :write_attribute :empty_string_nilling > > end > > http://weblog.rubyonrails.org/2006/4/26/new-in-rails-module-alias_met...http://errtheblog.com/posts/48-aliasmethodbling > > -- > Rick DeNatale > > My blog on Rubyhttp://talklikeaduck.denhaven2.com/--~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---