Nicknames do not solve your problem. As I mentioned in my comment above, you cannot have a dash in the ruby method or variable names, because ruby will interpret them as a minus. So:
object.listen-control
will be interpreted by the ruby as:
object.listen - control
and will fail. The detected code fragment may be unsuccessful due to ruby 1.9, and not rails 3. Ruby 1.9 does not allow you to call .send for protected or private methods anymore, for example 1.8.
At the same time, I understand that the times when the column names of the old database do not look very nice, and you want to clear them. Create a folder in the lib folder called "bellmyer". Then create a file called "create_alias.rb" and add the following:
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 an alias, you can do this:
class User < ActiveRecord::Base include Bellmyer::CreateAlias create_alias 'name-this', 'name_this' end
And that will be right. It uses the read_attribute and write_attribute ActiveRecord methods to access these table columns without calling them like ruby methods.
Jaime Bellmyer Oct 25 '10 at 13:37 2010-10-25 13:37
source share