Ruby on rails 获得';未初始化常数';在模型中使用委托时出错
我正在一个模型上实现一个视图,该模型有三个表,其中一个是联接表。以下是表格: 配方:Ruby on rails 获得';未初始化常数';在模型中使用委托时出错,ruby-on-rails,Ruby On Rails,我正在一个模型上实现一个视图,该模型有三个表,其中一个是联接表。以下是表格: 配方: class Recipe < ActiveRecord::Base # validates :name, :presence => true # validates :directions, :presence => true # has_and_belongs_to_many :ingradients has_many :ingredients_recipes ha
class Recipe < ActiveRecord::Base
# validates :name, :presence => 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
当我删除该行时,它运行时不会出错。我是否对代表的定义有误,或者是什么原因导致了这种情况?我认为您属于IngCredits中的关联规则应该是单数,而不是复数,例如:
class IngredientsRecipe < ActiveRecord::Base
belongs_to :recipe
belongs_to :ingredient, :foreign_key => "ingredient_id"
delegate :name, :to => :ingredient
end
class IngCreditsRecipe“配料id”
委托人:姓名,:收件人=>:成分
结束
此外,请检查您的类名的大小写。它可以是单数形式(IngredEnterprise),也可以是复数形式(IngredEnterprises)。。。我不确定是哪一种,但我知道不应该混用
最后,为什么要使用联接模型?如果IngCreditsRecipes中没有任何其他属性,只需像注释掉的行一样使用HABTM。+1表示单数/复数提示。。。这也使得
:foreign_key
的使用变得毫无用处。这就成功了-非常感谢。关于类名,我是通过scaffolding来实现的,因为它是一个连接表,所以我必须将表名的复数形式和rails自动从第二个单词中去掉复数形式,因此IngreditsRecipe。
class IngredientsRecipe < ActiveRecord::Base
belongs_to :recipes
belongs_to :ingredients, :foreign_key => "ingredient_id"
delegate :name, :to => :ingredients
end
<% @recipe.ingredients_recipes.each do |r| %>
<%= r.ingredient_id %> <br>
<%= r.name %>
<% end %>
uninitialized constant IngredientsRecipe::Ingredients
class IngredientsRecipe < ActiveRecord::Base
belongs_to :recipe
belongs_to :ingredient, :foreign_key => "ingredient_id"
delegate :name, :to => :ingredient
end