我正在尝试为我的类创建一个has_many:through关系,但对如何实际实现相关数据的保存和检索感到困惑。
我有一个Recipe类,Ingredient类和Amount类,它是:通过关联。以下是我们现在的课程:
#models/recipe.rb
class Recipe < ActiveRecord::Base
has_many :amounts
has_many :ingredients, :through => :amounts
end
#models/ingredient.rb
class Ingredient < ActiveRecord::Base
has_many :amounts
has_many :recipes, :through => :amounts
end
#models/amounts.rb
class Amounts < ActiveRecord::Base
belongs_to :recipes
belongs_to :ingredients
end
此外,这里有一个指向创建食谱的基本形式要点的链接:https://gist.github.com/2820455
创作代码本身的要点: https://gist.github.com/2820294
当我提交表单时,我收到此错误:
uninitialized constant Recipe::Amount
和这个应用程序跟踪:
app/controllers/recipes_controller.rb:57:in 'block in create'
app/controllers/recipes_controller.rb:56:in 'create'
我对Ruby on Rails非常陌生并且实现了这种关系,并且将Amounts值插入到表中令人难以置信......!
答案 0 :(得分:0)
belongs_to需要以您的金额为单位.rb将其更改为: -
class Amounts < ActiveRecord::Base
belongs_to :recipe
belongs_to :ingredient
end