I have the following models
class Recipe < ActiveRecord::Base
has_many :recipe_ingredients
has_many :ingredients, through: :recipe_ingredients
end
class Ingredient < ActiveRecord::Base
has_many :recipe_ingredients
has_many :recipes, through: :recipe_ingredients
end
class RecipeIngredient < ActiveRecord::Base
belongs_to :recipe
belongs_to :ingredient
end
I have checked through rails console that this has_many through many-to-many relation works well.
There is one attribute in the join table RecipeIngredient, in schema it is:
t.text "amount"
I would like to display the name from Ingredient and the amount from RecipeIngredient associated with Recipe.
I have included the following lines in RecipeController's show action:
def show
@recipe = Recipe.includes(:ingredients).find(params[:id])
end
Here's recipes/show.html.erb:
<% title @recipe.name %>
<div class="col-md-12">
<div class="group">
<%= link_to("Edit", edit_recipe_path(@recipe), class: "btn btn-primary pull-right")%>
</div>
<h2><%= @recipe.name %></h2>
<p><%= @recipe.description %></p>
<ul>Ingredients:
<% @recipe.ingredients.each do |i| ri = i.recipe_ingredients.where("recipe_id: ?", @recipe.id) %>
<li><%= link_to(i.name, ingredient_path(i)) %></li>
<p><%= ri.amount %></p>
<% end %>
</div>
The above code works well without the join table part. But after it was introduced, an NoMethodError in Recipes#show is raised, telling this is an undefined method.
How can I access the value then?