当你创建一个ActiveRecord::Base 的子类时,实际上是包装一个数据库表。缺省情况下,
Active Record 假定表名字是类名字的复数形式。如果类名包含多个以大写字母开头的单词,
表名会假定以下划线分隔这些单词。一些无规律的复数形式也会被处理。
这些规则反应了DHH(rails 的作者)的理念:类名字应该是单数,而表名字应该是复数。
如果你不喜欢这种做法,你可以在配置文件中设置一个全局变量关闭它(config 目录下的
environment.rb 文件)。
ActiveRecord::Base.pluralize_table_names = false
用于让表名字成为复数的算法很简单。多数时候它会正常工作,便如何你的类名字是
Sheep,它会试着查找名为sheeps 的表。对表名字和类名字的这种假设关系也可能会出问题,
如果你用个先前的schema 操作的话,[The meaning of the word schema varies across
the industry. We use it to mean the definition of tables and their
interrelationships in the context of an application or suite of related
applications. Basically, the schema is the database structure required by your
code. ]否则表的名字可能强迫你在代码中写陌生的和不合需要的类名字。基于这个原因,
Active Record 允许你使用set_table_name 指令地覆写缺省生成的表名字。
class Sheep < ActiveRecord::Base
set_table_name "sheep" # Not "sheeps"
end
class Order < ActiveRecord::Base
set_table_name "ord_rev99_x" # Wrap a legacy table...
end
Active Record 假定表名字是类名字的复数形式。如果类名包含多个以大写字母开头的单词,
表名会假定以下划线分隔这些单词。一些无规律的复数形式也会被处理。
这些规则反应了DHH(rails 的作者)的理念:类名字应该是单数,而表名字应该是复数。
如果你不喜欢这种做法,你可以在配置文件中设置一个全局变量关闭它(config 目录下的
environment.rb 文件)。
ActiveRecord::Base.pluralize_table_names = false
用于让表名字成为复数的算法很简单。多数时候它会正常工作,便如何你的类名字是
Sheep,它会试着查找名为sheeps 的表。对表名字和类名字的这种假设关系也可能会出问题,
如果你用个先前的schema 操作的话,[The meaning of the word schema varies across
the industry. We use it to mean the definition of tables and their
interrelationships in the context of an application or suite of related
applications. Basically, the schema is the database structure required by your
code. ]否则表的名字可能强迫你在代码中写陌生的和不合需要的类名字。基于这个原因,
Active Record 允许你使用set_table_name 指令地覆写缺省生成的表名字。
class Sheep < ActiveRecord::Base
set_table_name "sheep" # Not "sheeps"
end
class Order < ActiveRecord::Base
set_table_name "ord_rev99_x" # Wrap a legacy table...
end