成分的配方模型和为创建shoppinglist

编程入门 行业动态 更新时间:2024-10-28 16:26:45
本文介绍了成分的配方模型和为创建shoppinglist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个shoppinglist模型,其中有许多食谱,它也有许多成分。

I Have a shoppinglist model which has many recipes, which again has many ingredients.

class Shoppinglist < ActiveRecord::Base has_many :shoppinglistrecipezations has_many :recipes, :through => :shoppinglistrecipezations end class Recipe < ActiveRecord::Base has_many :shoppinglistrecipezations has_many :shoppinglists, :through => :shoppinglistrecipezations has_many :ingredients, :through => :ingredienzations has_many :ingredienzations end class Ingredient < ActiveRecord::Base has_many :recipes, :through => :ingredienzations has_many :ingredienzations end

当我添加多个配方的购物模式(其中一些有相同的成分),我想打印在shoppinglist和配料适量所有ingrediens的列表。 Shoppinglistingredienzations有一个整数人,告诉有多少人曾应计算和配方有一个人的变量来显示有多少人的食谱是。 Ingredienzations含有量,测量类型(克,茶匙等)和recipe_id

When I add several recipes to the shopping model (some of them have the same ingredients) I want to print a list of all the ingrediens in the shoppinglist and the right amount of ingredients. Shoppinglistingredienzations has an integer "Persons" to tell how many persons served should be calculated for and Recipe has a persons variable to show how many persons the recipe is for. Ingredienzations contains the amount, measurement type (grams, teaspoons etc.) and recipe_id.

推荐答案

你可以这样做:

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.

更多推荐

成分的配方模型和为创建shoppinglist

本文发布于:2023-10-17 02:43:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1499534.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:配方   成分   模型   shoppinglist

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!