2012-06-09 58 views
0

我有一個配方,成分,Ingredient_Recipe模型rails - 如何顯示在ui中使用多對多關聯模型創建的多對多數據模型?

配方有

​​

成分具有

has_many :ingredient_recipes 
has_many :Recipes, :through => :RecipeIngredient 

Ingredient_Recipe有

belongs_to :recipes 
belongs_to :ingredients 
在我的UI

這doenst工作了

<% @recipe.ingredients.each do |ingredient| %> 

編輯

ActionView::Template::Error (uninitialized constant Recipe::Ingredients): 
97:      </td> 
98:      <tr> 
99:      <td > 
100:       <% @recipe.ingredients.each do |ingredient| %> 
101:        ingredient.name 
102:       <% end %> 
103:      </td > 

回答

1

變化:

has_many :Ingredients, :through => :RecipeIngredient 

has_many :ingredients, :through => :ingredient_recipes 

不要大寫:配料和:通過需要引用你會聯想而不是模型。

:菜譜:

has_many :recipes, :through => :ingredient_recipes 
+0

感謝。現在我得到了 - 找不到關聯:RecipeIngredient模型食譜在行<%@ recipe.ingredients.each do |成分| %>(也在調試時,我看到@ associationHAsh =空哈希) – Jeb

+0

對不起,我的錯誤,我會更新答案。 –

+0

做明顯並將其更改爲IngredientRecipe我收到一條無法評估的消息 – Jeb