Ruby on Rails - 定义模型或表的复数名称

Joã*_*nha 1 ruby model ruby-on-rails plural

我有这个模型

class Oferta < ActiveRecord::Base
  belongs_to :entidade
  has_many :candidatos, :through => :interesses
  has_many :interesses, foreign_key: "oferta_id", dependent: :destroy
Run Code Online (Sandbox Code Playgroud)

基本上我有这个模型和模型Interesse及其复数,interesses但我认为Rails实际上是es在最后取消 并留给我Interess.现在它给了我这个错误:

uninitialized constant Oferta::Interess
Run Code Online (Sandbox Code Playgroud)

我如何定义的单数interessesinteresse?并不是interess

Ser*_*yol 10

你可以在你的config/initializers/inflections.rb文件中做这样的事情.

ActiveSupport::Inflector.inflections do |inflect|
 inflect.irregular 'interesse', 'interesses'
end
Run Code Online (Sandbox Code Playgroud)

  • 根据以上链接,您的自定义变形应位于`config/initializers/inflections.rb`中 (4认同)
  • 是的把它放在inflections.rb是一个最好的做法.谢谢@Rahul (2认同)