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()).
On Fri, Apr 4, 2008 at 6:31 AM, apramanik [email protected] wrote:
alias write_attribute_internal write_attribute
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
–
Rick DeNatale
My blog on Ruby
http://talklikeaduck.denhaven2.com/
Haha, it was just a test to see if it would work. Thanks for the info!