Sunday, 15 February 2015

ruby on rails - Iterate over has_many through relationship and include data from joining table -


I have a very simple rail app with three models: ingredients, materials, and ingredient table prescription in the recipe a recipe For, I want to list all the accessories and the amount found in the joining table. How can I be rewritten on the content, but it also includes data from the volume table?

  class recipe & lt; ActiveRecord :: Base has has_many: quantity has_many: content, through: quantity accepts_nested_attributes_for: quantity,: reject_if = & gt; : All_blank ,: allow_destroy = & gt; True end  

and:

  class component  

and finally include in the table:

  class volume & lt ; ActiveRecord :: Base is_to: recipe is_to: ingredient accepts_nested_attributes_for: ingredient, allow_destroy: true end It seems that it should be really easy to reproduce, but I'm not sure how.  

show.html ARB:

  & lt;% @ recipe.ingredients.each do | Component | & Gt%; & Lt;% # I know the line given below is incorrect, but it is not sure how to repeat the content again for # recipe and add the zodiac field # as well as the ingredient name from the #in quantity table & Gt%; & Lt; Li & gt; & Lt;% = ingredient.amount element.name% & gt; & Lt; / Li & gt; & Lt;% end% & gt;  

Thanks!

Do something in your controller's action:

  @ Recipe = Recipe.includes (: content, quantity) .find (param [: id]) # Avoid N + 1  

And then, in your opinion:

  & lt;% @ recipe.quantities.each do | Volume | & Gt%; & Lt;% = quantity.indereat.name%> - & lt;% = volume.mount% & gt; & Lt;% end% & gt;  

No comments:

Post a Comment