I’ve got a legacy database with a bunch of idiotically named columns like:
some_field_c some_other_field_c a_third_field_c
I would very much like to make a Rails ActiveRecord sub-class that automatically aliases these attributes to their name minus the underscore and ‘c’. However, when I tried:
attributes.each_key do | key | name = key alias_attribute key.to_sym, key[0, (key.length -2)].to_sym if key =~ /_c$/ end
in my class definition I got an ‘undefined local variable or method `attributes” error. I also tried overwriting these methods:
method_missing respond_to?
but I kept getting errors with that route too.
So my question (actually questions) is/are:
- Is what I’m trying to do even possible?
- If so, what’s the best way to do it (iterative aliasing or overwriting method missing)?
- If it’s not too much trouble, a very brief code sample of how to do #2 would be awesome.
Thanks in advance for any answers this post receives.
Your problem is probably that
attributesis an instance method and you’re doing this in the context of the class. The class method that’s closest to what you want iscolumn_names.