Ruby on Rails:如何在 Rails 中明确定义复数名称和单数名称? [英] Ruby on Rails: How do you explicitly define plural names and singular names in Rails?

查看:33
本文介绍了Ruby on Rails:如何在 Rails 中明确定义复数名称和单数名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我使用奖金"作为我的模型,所以我希望奖金"是复数形式,而奖金"是单数形式.

For example, I'm using "Bonus" as my model, so I'd expect "bonuses" to be the plural form and "bonus" to be the singular form.

然而,在 Ruby 中,这会导致:

However, in Ruby, this results in:

"bonus".pluralize # bonus
"bonuses".singularize # bonuse

因此,例如,当我执行has_many :bonuses"时,它不使用 Bonus.rb 模型(因为 Ruby 期望使用 Bonuse.rb 模型).有没有办法在 Ruby on Rails 中以某种方式纠正这个问题,使bonuses"充当模型 bonus.rb 的复数形式?

So, when I do a "has_many :bonuses", for example, it doesn't use the Bonus.rb model (since Ruby expects a Bonuse.rb model instead). Is there a way to correct that in Ruby on Rails somehow such that "bonuses" acts as the plural form for the model bonus.rb?

推荐答案

在 config/initializers 中,您将找到一个名为 inflections.rb 的文件.这里有一些说明,但您可能需要以下内容:

In config/initializers, you will find a file called inflections.rb. There are some instructions in here, but you will want something along the lines of:

ActiveSupport::Inflector.inflections do |inflect|
  inflect.irregular 'bonus', 'bonuses'
end

这篇关于Ruby on Rails:如何在 Rails 中明确定义复数名称和单数名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆