别名Rails中列名别名、Rails、中列名

2023-09-08 15:41:16 作者:人潮拥挤我像多余

在我的数据库中有列名,如删除或监听控制等。这些不能改变,所以我想别名的名称,以避免在我的应用程序的问题。

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.

我发现以下code 但它是过时的(2005年8月5日),不使用Rails 3工作:

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?

推荐答案

这个声明在模型中。

alias_attribute :new_column_name, :column_name_in_db