【问题标题】:Ruby on Rails - Define plural name of Model or TableRuby on Rails - 定义模型或表的复数名称
【发布时间】:2014-08-28 08:48:53
【问题描述】:

我有这个模型

class Oferta < ActiveRecord::Base
  belongs_to :entidade
  has_many :candidatos, :through => :interesses
  has_many :interesses, foreign_key: "oferta_id", dependent: :destroy

基本上我有这个模型和模型Interesse,它的复数是interesses,但我认为Rails 实际上在最后取消了es,让我有了兴趣。现在它给了我这个错误:

uninitialized constant Oferta::Interess

如何定义interesses 的单数是interesse?而不是interess

【问题讨论】:

标签: ruby-on-rails ruby model plural


【解决方案1】:

您可以在 config/initializers/inflections.rb 文件中执行类似操作。

ActiveSupport::Inflector.inflections do |inflect|
 inflect.irregular 'interesse', 'interesses'
end

【讨论】:

  • 是的。我看到了上面的链接。但是,是的,这几乎就是我所做的。感谢你的回答。我会尽快接受的
  • 根据上面的链接,您的自定义变形应该驻留在config/initializers/inflections.rb
  • 是的,把它放在 inflections.rb 中是一个最佳实践。谢谢@Rahul
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-02
  • 2020-04-23
相关资源
最近更新 更多