ActiveRecord 如何从单数(类)和复数(表)推断映射;是否可以覆盖?
例如,ActiveRecord 如何从单数(类)和复数(表)推断映射:
People = Person
Ducks = Duck
Geese = Goose
Categories = Category
在概念上似乎是一个好主意,但不知道我是否必须映射单数(类)和复数(表)实例,或者 ActiveRecord 是否是这样做是多么“神奇”。另外,当必须对名称进行复数化时,这似乎可能会导致更多的开销,而这不仅仅是添加“s”那么简单。
注意:将此问题的第二部分移至此处:ActiveRecord 是否使用命名约定“ID”为每个表分配一个键,如果是,为什么?
How does ActiveRecord infer mapping from singular (class) and plural (table) for example:
People = Person
Ducks = Duck
Geese = Goose
Categories = Category
Seems like a nice idea in concept, but no idea if I have to map the singular (class) and plural (table) instances, or if ActiveRecord is some how "magically" doing this. Plus, seems like this might lead to more overhead when having to pluralize a name that's not as simple as just adding "s".
NOTE: Moved the second part of this question here: Does ActiveRecord assign a key to every table using the naming convention “ID”, and if so, why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Rails 使用 ActiveSupport::Inflector 来跟踪单词以及如何更改它们。例如,Rails 会知道将 UserPreference 存储在 user_preferences 中。
您还可以将您的一种变形添加到变形器中,以处理添加 s 没有意义的更奇怪的情况。
在 /config/initializers/inflections.rb 中,您将获得以下内容:
http://api.rubyonrails.org/classes/ ActiveSupport/Inflector.html
Rails uses the ActiveSupport::Inflector to keep track of words and how to change them. For example, Rails will know to store UserPreference in user_preferences
You can also add your one inflections to the inflector to handle weirder cases where adding an s does not make sense.
In /config/initializers/inflections.rb you get the following:
http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html