1

我正在尝试为我的类创建一个 has_many :through 关系,但对如何实际实现相关数据的保存和检索感到困惑。

我有一个食谱类、成分类和数量类,它们是关联的 :through 。这是我现在的课程:

#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 值插入表中让我难以置信......!

4

1 回答 1

0

belongs_to 需要是单数,在你的 amount.rb 中将其更改为如下所示:-

模型/数量.rb

class Amounts < ActiveRecord::Base
  belongs_to :recipe
  belongs_to :ingredient
end
于 2012-05-28T19:17:46.770 回答