Aliasing method names won't solve your problem. As I mentioned in my comment above, you can't have dashes in ruby method or variable names, because ruby will interpret them as a "minus". so:
object.listen-control
will be interpreted by ruby as:
object.listen - control
and will fail. The code snippet you found might be failing because of ruby 1.9, not rails 3. Ruby 1.9 doesn't let you call .send
on protected or private methods anymore, like 1.8 used to.
That being said, I do understand there are times when old database column names don't look very nice, and you want to clean them up. Create a folder in your lib folder called "bellmyer". Then create a file called "create_alias.rb", and add this:
module Bellmyer
module CreateAlias
def self.included(base)
base.extend CreateAliasMethods
end
module CreateAliasMethods
def create_alias old_name, new_name
define_method new_name.to_s do
self.read_attribute old_name.to_s
end
define_method new_name.to_s + "=" do |value|
self.write_attribute old_name.to_s, value
end
end
end
end
end
Now in your model that needs aliasing, you can do this:
class User < ActiveRecord::Base
include Bellmyer::CreateAlias
create_alias 'name-this', 'name_this'
end
And it will alias properly. It's using the read_attribute
and write_attribute
methods of ActiveRecord to access those table columns without calling them as ruby methods.