Rails 4 - Access Join Table Value in views

2019-09-17 07:44发布

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?

2条回答
女痞
2楼-- · 2019-09-17 08:05
@recipe.ingredients.select('ingredients.*, recipe_ingredients.amount').each do |i|
  <li><%= link_to(i.name, ingredient_path(i)) %></li>
  <p><%= i.amount %></p>
end

-- may be u can use delegates for that particular attribute "amount"

查看更多
狗以群分
3楼-- · 2019-09-17 08:14

If u want to make join table attributes as editable fields, u need to use nested attributes.

In Recipe Model

accepts_nested_attributes_for :recipe_ingredients

and in the view

fields_for :recipe_ingredients
# f.field :amount
查看更多
登录 后发表回答