开发者

Alias for column names in Rails

开发者 https://www.devze.com 2023-01-21 02:21 出处:网络
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.

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 co开发者_如何转开发lumn names? Is it possible?


Declare this in your model.

alias_attribute :new_column_name, :column_name_in_db


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消