0

has_manyがコレクションに提供するメソッド(「where」など)を使用すると、ビューに次のエラーが表示される理由がわかりません。

History / hist_paquets_mesures#showのNameErrorは、/var/www-opf/opf/app/views/history/hist_paquets_mesures/show.html.hamlを表示しています。ここで、行#19が発生しました: 初期化されていない定数HistPaquetMesures :: HistVersionsPaquetsMesure

この問題を引き起こしている私の(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/inflections.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"

私のapp/model / hist_paquet_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

私のapp/model / hist_version_paquet_mesures.rbモデル:

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

私のapp/controllers / history / hist_paquets_mesures_controller.rbコントローラー:

class History::HistPaquetsMesuresController < ApplicationController
  def show
    @hist_paquet_mesures = HistPaquetMesures.find_by_id(params[:id])
    respond_with(:history, @hist_paquet_mesures)
  end
end

奇妙なことは、エラーメッセージの「HistVersionsPaquetsMesure」の「s」がどこにあるかです:HistPaquetMesures :: HistVersionsPaquetsMesure

HistPaquetMesures :: HistVersionPaquetMesuresではないでしょうか?なぜこの結果が得られるのですか?

どんな助けでも大歓迎です

4

2 に答える 2

1

以下で :class_name パラメータを使用してみてください:

has_many :hist_versions_paquets_mesures, :class_name => HistVersionPaquetMesures

協会の名前に問題があるかもしれません。

于 2013-01-14T11:02:23.360 に答える
0

バグです!

この件について問題を提起しました。問題は ActiveSupport ライブラリにあるようです。クラス名を取得するには、「camelize.singularize」を実行します。語尾変化がアンダースコアで定義されているため、期待される結果が正しくありません。ライブラリが「singularize.camelize」を実行していれば機能していたでしょう。

回避策 : ラクダ化された構文で別の不規則な語形変化を追加します。

于 2013-01-14T13:07:37.000 に答える