Skip to content Skip to sidebar Skip to footer

Sum Of Ingredients In Recipes Model To Create Shoppinglist

I Have a shoppinglist model which has many recipes, which again has many ingredients. class Shoppinglist < ActiveRecord::Base has_many :shoppinglistrecipezations has_many

Solution 1:

You could do this:

Shoppinglist.where(id: list_id).joins({recipes: : ingredients}).group('ingredients.name').sum('ingredients.amount')

assuming your Ingredient model has the attributes amount and name. It becomes much more tricky if you have units on you amounts. But this query can be rewritten to make that work as well.

Post a Comment for "Sum Of Ingredients In Recipes Model To Create Shoppinglist"