Ruby-on-rails 如何在 Rails 中显式指定模型的表名映射?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4613574/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:01:29  来源:igfitidea点击:

How do I explicitly specify a Model's table-name mapping in Rails?

ruby-on-rails

提问by Eran Kampf

I have a Model class called Countries and I want it to map to a DB table called 'cc'.

我有一个名为“国家”的模型类,我希望它映射到一个名为“cc”的数据库表。

How is that done in Rails?

在 Rails 中是如何做到的?

回答by Zabba

Rails >= 3.2 (including Rails 4+ and 5+):

Rails >= 3.2(包括 Rails 4+ 和 5+):

class Countries < ActiveRecord::Base
  self.table_name = "cc"
end

Rails <= 3.1:

导轨 <= 3.1:

class Countries < ActiveRecord::Base
  self.set_table_name "cc"
  ...
end

回答by chelofm

class Countries < ActiveRecord::Base
    self.table_name = "cc"
end

In Rails 3.x this is the way to specify the table name.

在 Rails 3.x 中,这是指定表名的方式。