在模型中的belongs_to中使用委托时,会出现“未初始化的常量”错误

我正在使用三个表实现一个模型的视图,其中一个表连接表。 以下是表格:

食谱:

class Recipe  true # validates :directions, :presence => true # has_and_belongs_to_many :ingradients has_many :ingredients_recipes has_many :ingredients, :through => :ingredients_recipes accepts_nested_attributes_for :ingredients_recipes, :allow_destroy => true end 

成分:

 class Ingredient  :ingredients_recipes end 

它们之间的连接表:

 class IngredientsRecipe  "ingredient_id" delegate :name, :to => :ingredients end 

这似乎工作正常,当我创建一个新的配方 – 我可以编辑配方行,一切运行顺利。 当我创建一个视图以使用Ingredients表中的成分名称显示配方时,我在连接表中创建了一个委托。 但是,当我尝试在视图中使用委托时:

    

我收到以下错误:

 uninitialized constant IngredientsRecipe::Ingredients 

当我删除行时,它运行没有错误。 我是否错误地定义了代表或者导致这种情况的原因是什么?

我相信在IngredientsRecipes中你的belongs_to关联应该是单数,而不是复数,例如:

 class IngredientsRecipe < ActiveRecord::Base belongs_to :recipe belongs_to :ingredient, :foreign_key => "ingredient_id" delegate :name, :to => :ingredient end 

另外,请检查您的class级名称。 它应该是所有单数(IngredientRecipe),或所有复数(IngredientsRecipes)……我不确定哪个,但我知道它不应该混合。

最后,为什么使用连接模型? 如果您在IngredientsRecipes中没有任何其他属性,只需像注释掉的行一样使用HABTM。