Ruby-on-rails Rails 中列名的别名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4014831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Alias for column names in Rails
提问by user486421
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 codebut it is outdated (05 Aug 2005) and doesn't work with Rails 3:
我找到了以下代码,但它已过时(2005 年 8 月 5 日)并且不适用于 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?
如何为列名设置别名?是否可以?
回答by Shreyas
Declare this in your model.
在您的模型中声明这一点。
alias_attribute :new_column_name, :column_name_in_db
回答by Jaime Bellmyer
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:
别名方法名称不会解决您的问题。正如我在上面的评论中提到的,在 ruby 方法或变量名称中不能有破折号,因为 ruby 会将它们解释为“减号”。所以:
object.listen-control
will be interpreted by ruby as:
将被 ruby 解释为:
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 .sendon protected or private methods anymore, like 1.8 used to.
并且会失败。由于 ruby 1.9 而不是 rails 3,您发现的代码片段可能会失败。Ruby 1.9 不再允许您调用.send受保护或私有方法,就像 1.8 过去那样。
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:
话虽如此,我确实理解有时旧的数据库列名看起来不太好,您想清理它们。在您的 lib 文件夹中创建一个名为“bellmyer”的文件夹。然后创建一个名为“create_alias.rb”的文件,并添加以下内容:
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_attributeand write_attributemethods of ActiveRecord to access those table columns without calling them as ruby methods.
它会正确地别名。它使用ActiveRecord的read_attribute和write_attribute方法来访问这些表列,而无需将它们作为 ruby 方法调用。
回答by Ariejan
As Jaime said, those names might cause problems.
正如 Jaime 所说,这些名字可能会引起问题。
In that case, use some sensible names. Your GUI should never dictate how your columns are named.
在这种情况下,请使用一些合理的名称。您的 GUI 永远不应该规定您的列的命名方式。
Suggestions: is_deletedor deleted_at, listen_control
建议:is_deleted或deleted_at,listen_control
Then, change your view accordingly, that's way easier than fighting ActiveRecord and your database.
然后,相应地更改您的视图,这比对抗 ActiveRecord 和您的数据库要容易得多。

