如何在Rails 5中从匹配ID的连接表中获取记录数(How to get count of records from join table with matching ID in Rails 5)

编程入门 行业动态 更新时间:2024-10-09 02:28:21
如何在Rails 5中从匹配ID的连接表中获取记录数(How to get count of records from join table with matching ID in Rails 5)

我有2个模型。 膳食和食物。 一顿饭可以有很多食物,一种食物可以是多餐的一部分。 他们有一个多对多的关联,由has_many:through完成。 连接模型称为MealFood,连接表称为meal_foods。 创建膳食时,您可以添加尽可能多的食物。 有一个jQuery按钮,当你点击它时会自动在窗体中添加一个新的输入,并在那里输入食物。 如何使膳食的编辑视图显示与食物项目名称一样多的输入字段。

膳食控制器:

def create @meal = current_user.meals.new(meal_params) if @meal.save @meal.update(total_calories: @meal.calc_total_calories, total_carbohydrates: @meal.calc_total_carbohydrates, total_proteins: @meal.calc_total_proteins, total_fat: @meal.calc_total_fat) redirect_to @meal else render 'new' end end def edit @meal = Meal.find(params[:id]) end def update if @meal.update(meal_params) @meal.update(total_calories: @meal.calc_total_calories, total_carbohydrates: @meal.calc_total_carbohydrates, total_proteins: @meal.calc_total_proteins, total_fat: @meal.calc_total_fat) redirect_to @meal else render 'edit' end end

膳食视图(创建操作):

<%= form_for(@meal) do |f| %> <div class="field"> <%= f.label :meal_type %> <%= f.select :meal_type, ["Breakfast", "Lunch", "Dinner", "Morning Snack", "Afternoon Snack", "Evening Snack"] %> </div> <div class="field"> <label class="input-dropdown">Food Item #1</label> <%= select_tag "meal[food_ids][]", options_from_collection_for_select(Food.all, "id", "name") %> </div> <div class="field submit"> <%= f.submit class: "button button-highlight button-block" %> </div> <% end %>

提前致谢!

I have 2 models. Meal and Food. A meal can have many food items and a food item can be a part of many meals. They have a many-to-many association, done by has_many :through. The join model is called MealFood and the join table is called meal_foods. When creating a meal you can add as many food items as you want. There's a jQuery button that when you click it automatically appends a new input in the form and you enter the food item there. How can I make the edit view for the meal to show as many input fields with the name of the food item as there really is.

Meals Controller:

def create @meal = current_user.meals.new(meal_params) if @meal.save @meal.update(total_calories: @meal.calc_total_calories, total_carbohydrates: @meal.calc_total_carbohydrates, total_proteins: @meal.calc_total_proteins, total_fat: @meal.calc_total_fat) redirect_to @meal else render 'new' end end def edit @meal = Meal.find(params[:id]) end def update if @meal.update(meal_params) @meal.update(total_calories: @meal.calc_total_calories, total_carbohydrates: @meal.calc_total_carbohydrates, total_proteins: @meal.calc_total_proteins, total_fat: @meal.calc_total_fat) redirect_to @meal else render 'edit' end end

Meals View (create action):

<%= form_for(@meal) do |f| %> <div class="field"> <%= f.label :meal_type %> <%= f.select :meal_type, ["Breakfast", "Lunch", "Dinner", "Morning Snack", "Afternoon Snack", "Evening Snack"] %> </div> <div class="field"> <label class="input-dropdown">Food Item #1</label> <%= select_tag "meal[food_ids][]", options_from_collection_for_select(Food.all, "id", "name") %> </div> <div class="field submit"> <%= f.submit class: "button button-highlight button-block" %> </div> <% end %>

Thanks in advance!

最满意答案

由于这是使用连接表,我假设你不希望用户真的能够编辑Food记录本身 - 他们应该编辑MealFood记录,对吧?

在这种情况下,您应该能够查询meal.meal_foods以获取要编辑的连接表记录。 您的查询可能如下所示:

@meal_foods = @meal.meal_foods.includes(:food)

.includes(:food)会自动加载您需要的所有食物记录,防止您遇到N + 1查询问题。

然后在你看来,你可以做这样的事情:

<div class="field"> <% @meal_foods.each do |mf| %> <... your input element for each record /> <% end %> </div>

如果您确实希望自己的用户能够修改Food记录,则可以使用相同的策略,但只需使用已创建的through关联。

Since this uses a join table, I assume you don't want users to actually be able to edit the Food record itself - they should be editing the MealFood record, right?

In that case, you should be able to query meal.meal_foods to get the join table records you want to edit. Your query might look something like this:

@meal_foods = @meal.meal_foods.includes(:food)

The .includes(:food) will auto-load all the food records you need, preventing you from having an N+1 query problem.

Then in your view, you can do something like this:

<div class="field"> <% @meal_foods.each do |mf| %> <... your input element for each record /> <% end %> </div>

If you do want your users to be able to modify the Food records themselves, then you can use the same strategy, but just use the through association you already created.

更多推荐

本文发布于:2023-07-27 08:31:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1287761.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   ID   Rails   count   matching

发布评论

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

>www.elefans.com

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