単数形/複数形の変換ルールを独自に定義する
必要に迫られたので探してました. 案の定用意されたメソッドで好き勝手できるようになってました.
Rails御用達のActiveSupportの場合.
require 'active_support' require 'active_support/inflector' # Railsは自動で取り込んでくれるだろうけど, ActiveSupportを単体で使う場合は取り込んでくれないみたいです ActiveSupport::Inflector.inflections do |inflect| inflect.irregular 'octopus', 'octopi' end ActiveSupport::Inflector.inflections.irregular 'octopus', 'octopi' # コレでも一緒
イレギュラーなケース以外にもいろいろと定義できるので, ActiveSupport::Inflector::Inflectionsのドキュメントなり読みましょう.
Sequelもモデル名とテーブル名の変換に同様の仕組みを用いてる.
require 'sequel' Sequel.inflections do |inflect| inflect.irregular 'octopus', 'octopi' end