Rails 単数形と複数形 - izudon/izudon.github.io GitHub Wiki
結論
- 単数系と複数形を変換するには
String
の.pluralize
.singularize
を用いる。 - 不規則変化は
config/initializers/inflections.rb
に登録しておける。 - 登録されていない不規則変化では変換ミスもしばしば起こる。
tooth
->tooths
/chaches
->cach
など。
不規則変化登録の例
ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.irregular 'uta', 'uta'
# inflect.plural /^(ox)$/i, '\1en'
# inflect.singular /^(ox)en/i, '\1'
# inflect.irregular 'person', 'people'
# inflect.uncountable %w( fish sheep )
end