Methods 为什么;“未初始化常量”;使用has\u多个关联集合时出错(模型以s bug结尾)?

Methods 为什么;“未初始化常量”;使用has\u多个关联集合时出错(模型以s bug结尾)?,methods,ruby-on-rails-3.2,model-associations,Methods,Ruby On Rails 3.2,Model Associations,我找不到为什么在使用对其集合(例如“where”)提供了很多的方法时,我的视图中会出现以下错误: 历史/历史记录中的名称错误 显示/var/www-opf/opf/app/views/history/hist_-paquets_-mesures/show.html.haml,其中第19行出现: 未初始化常量HistPaquetMesure::HistVersionPaquetMesure 我的(HAML)视图中导致此问题的部分: // I want to make this work //= @

我找不到为什么在使用对其集合(例如“where”)提供了很多的方法时,我的视图中会出现以下错误:

历史/历史记录中的名称错误 显示/var/www-opf/opf/app/views/history/hist_-paquets_-mesures/show.html.haml,其中第19行出现: 未初始化常量HistPaquetMesure::HistVersionPaquetMesure

我的(HAML)视图中导致此问题的部分:

// I want to make this work
//= @hist_paquet_mesures.hist_versions_paquets_mesures.where(:hist_origine_modification == nil).first.version
// So I debug with this, which is causing the same error
= debug @hist_paquet_mesures.hist_versions_paquets_mesures
我为我在config/initializers/definctions.rb中使用的特殊单数复数名称设置了一个例外:

  ActiveSupport::Inflector.inflections do |inflect|
    inflect.irregular 'pub_liste_horizon', 'pub_listes_horizon'
    inflect.irregular 'hist_paquet_mesures', 'hist_paquets_mesures'
    inflect.irregular 'hist_projet_connexe', 'hist_projets_connexes'
    inflect.irregular 'hist_version_paquet_mesures', 'hist_versions_paquets_mesures'
    inflect.irregular 'hist_origine_modification', 'hist_origines_modification'
  end
多重化单一化分类方法在rails控制台上按预期工作:

"hist_versions_paquets_mesures".singularize  => "hist_version_paquet_mesures"
"hist_versions_paquets_mesures".classify  => "HistVersionPaquetMesures"
"hist_version_paquet_mesures".pluralize  => "hist_versions_paquets_mesures"
我的应用程序/模型/历史测量。rb模型:

class HistPaquetMesures < ActiveRecord::Base
  belongs_to :pub_indice
  belongs_to :pub_liste_horizon
  belongs_to :admin_utilisateur
  has_many :hist_versions_paquets_mesures
end
class HistVersionPaquetMesures < ActiveRecord::Base
  belongs_to :hist_paquet_mesures
  belongs_to :pub_modification
  belongs_to :vers_origine, :class_name => 'HistOrigineModification', :foreign_key => 'hist_origine_modification_id'
  # polymorphic association
  has_one :comme_origine, :class_name => 'HistOrigineModification', as: :hist_origine
end
类HistPaquetMesures 我的应用程序/模型/历史版本\u paquet\u测量。rb模型:

class HistPaquetMesures < ActiveRecord::Base
  belongs_to :pub_indice
  belongs_to :pub_liste_horizon
  belongs_to :admin_utilisateur
  has_many :hist_versions_paquets_mesures
end
class HistVersionPaquetMesures < ActiveRecord::Base
  belongs_to :hist_paquet_mesures
  belongs_to :pub_modification
  belongs_to :vers_origine, :class_name => 'HistOrigineModification', :foreign_key => 'hist_origine_modification_id'
  # polymorphic association
  has_one :comme_origine, :class_name => 'HistOrigineModification', as: :hist_origine
end
class HistVersionPaquetMesures'HistorEngineModification',:foreign\u-key=>'hist\u-origine\u-modification\u-id'
#多态关联
有一个:comme\u origine,:class\u name=>'historoginemodification',as::hist\u origine
结束
My应用程序/控制器/历史记录/历史记录测量控制器.rb控制器:

class History::HistPaquetsMesuresController < ApplicationController
  def show
    @hist_paquet_mesures = HistPaquetMesures.find_by_id(params[:id])
    respond_with(:history, @hist_paquet_mesures)
  end
end
类历史::HistPaquetsMesuresController
奇怪的是,错误消息的“HistVersionsPaquetMessure”中的“s”在哪里:HistPaquetMessure::HistVersionsPaquetMessure

它不应该是HistPaquetMesures::HistVersionPaquetMesures吗? 为什么我会有这个结果


欢迎提供任何帮助

尝试在以下位置使用:class\u name参数:

has_many :hist_versions_paquets_mesures, :class_name => HistVersionPaquetMesures
您的协会名称可能有问题。

这是一个BUG

我打开了一个关于这个案子的报告。 问题似乎来自ActiveSupport库。 为了获得类名,它执行“camelize.singularize”。由于我的词形变化是用下划线定义的,因此预期结果不正确。如果图书馆执行“奇点化.骆驼化”的话,它会起作用的


解决方法:添加另一个带有camelized语法的不规则屈折变化。

您的解决方案确实有效。谢谢为什么会这样。我的协会名称哪里会有问题?我认为你不应该用复数来表示你模特的名字。我会这样命名我的模型:HistPaquetMesure,HistVersionPaquetMesure,并且在我的HistPaquetMesure模型中有很多这样的关联:
有很多:HistPaquetMesure
。但我不知道你们在做什么,参考这些模型的名称,所以也许你们不会改变它们。我会听从你们的建议,尽管它在法语中的语法不正确。我桌子上的一个记录是关于“一个包有许多度量单位”的。法语的复数形式是“许多包有许多度量单位”。非常感谢你能告诉我你的桌子的名字。我是法国人,所以我可以帮助你。为什么我的协会会朝相反的方向工作?例如:我的历史版本。历史版本