81

In my database has column names such as 'delete' or 'listen-control' and so on. These cannot be changed, so I would like to alias the names so as to avoid problems in my application.

I found the following code but it is outdated (05 Aug 2005) and doesn't work with Rails 3:

module Legacy
  def self.append_features(base)
    super
    base.extend(ClassMethods)
  end
  module ClassMethods
    def alias_column(options)
      options.each do |new_name, old_name|
        self.send(:define_method, new_name) { self.send(old_name) }
        self.send(:define_method, "#{new_name}=") { |value| self.send("#{old_name}=", value) }
      end
    end
  end
end

ActiveRecord::Base.class_eval do
  include Legacy
end

How can I alias the column names? Is it possible?

BryanH
  • 5,826
  • 3
  • 34
  • 47
user486421
  • 839
  • 1
  • 6
  • 4
  • I don't see what the problem is with using 'delete' and 'listen-control' as column names? Do you run into an error or something else? – Ariejan Oct 25 '10 at 13:13
  • 5
    listen-control causes problems because it has a dash in the name, which makes it an invalid ruby object attribute. Ruby will interpret "object.listen-control" as "object.listen, minus control". And delete might be a reserved keyword. I don't know why you'd want to do this. Sometimes the right answer is to stop trying to fight ruby or rails. – Jaime Bellmyer Oct 25 '10 at 13:25
  • `define_method("listen-control", Proc.new { puts "bingo" })` then `send("listen-control")`. Where's the problem? – rthbound Jan 06 '17 at 15:40

3 Answers3

175

Declare this in your model.

alias_attribute :new_column_name, :column_name_in_db
Shreyas
  • 8,737
  • 7
  • 44
  • 56
12

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.

Jaime Bellmyer
  • 23,051
  • 7
  • 53
  • 50
  • Thanks for answer, but I'm tested your solution and get error: SyntaxError (/usr/lib/ruby/gems/1.9.1/gems/activemodel-3.0.0/lib/active_model/attribute_methods.rb:229: syntax error, unexpected ',' send(:vm-password=, *args) ^): when defined: alias_attribute "vm_password", "vm-password" and try to get "vm_password". – user486421 Oct 25 '10 at 15:00
  • Big thanks! I tested it and finds: 1. In user.rb needs add first line: require 'bellmyer/create_alias.rb' 2. When no one records in table, your method works fine. With one or more records rails gets error: ActionView::Template::Error (/usr/lib64/ruby/gems/1.9.1/gems/activemodel-3.0.0/lib/active_model/attribute_methods.rb:273: syntax error, unexpected '-', expecting keyword_end undef :vm-password?… 3. With field name such as 'delete' this method not works because 'delete' is method name. – user486421 Oct 25 '10 at 17:09
  • You can't use the old field names in your view. In my example above, you need to use "<%= user.name_this %>". You also need to custom_alias your delete field, and give it a different name. If you alias it to "delete_field" then you can call "<%= user.delete_field %>". – Jaime Bellmyer Oct 25 '10 at 20:09
  • I'm used new names of course. My migration: http://paste.pocoo.org/show/281427/ My controller: http://paste.pocoo.org/show/281428/ My model: http://paste.pocoo.org/show/281430/ My view: http://paste.pocoo.org/show/281433/ Error: http://paste.pocoo.org/show/281434/ Erro for enable in migration and aliased 'delete': http://paste.pocoo.org/show/281439/ Sorry for many pastes. – user486421 Oct 25 '10 at 20:52
  • 1
    +1 for a nice solution using the '.' operator but >> you can't have dashes in ruby method or variable names Isn't completely correct. You can have methods with -'s if you escape them and use send like so object.send(:'method-with-dashes'). – diedthreetimes Apr 12 '11 at 21:00
  • Nice solution, but provides inaccurate information. – rthbound Jan 06 '17 at 15:41
  • This is false: *"Ruby 1.9 doesn't let you call `.send` on protected or private methods anymore"*. On 2.3 and 2.4 `.send` works just fine for private methods. Perhaps `.public_send` was involved? – Beni Cherniavsky-Paskin Dec 28 '17 at 09:20
1

As Jaime said, those names might cause problems.

In that case, use some sensible names. Your GUI should never dictate how your columns are named.

Suggestions: is_deleted or deleted_at, listen_control

Then, change your view accordingly, that's way easier than fighting ActiveRecord and your database.

Ariejan
  • 10,910
  • 6
  • 43
  • 40