Rails,解决此n + 1的技巧? [英] Rails, tips for solving this n+1?

查看:78
本文介绍了Rails,解决此n + 1的技巧?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个员工视图,其中列出了所有技能,这些技能都写在数据库的技能表中.对于每个员工,所有技能都会显示出来,就像它想要的那样.

I have an employee view, where are listed all skills, which are written in the skills table on my db. For every employee, all skills are displayed, just as want it to.

员工和技能彼此之间是相互关联的,这与许多:through关联相关.

Employees and skills are related to each other as has many :through association.

class Employee < ApplicationRecord
  has_many :employeeskillsets, foreign_key: "employee_id"
  has_many :skills, through: :employeeskillsets
end

class Skill < ApplicationRecord
  has_many :employeeskillsets, foreign_key: "skill_id"
  has_many :employees, through: :employeeskillsets
end 

class Employeeskillset < ApplicationRecord
  belongs_to :employee, foreign_key: 'employee_id'
  belongs_to :skill, foreign_key: 'skill_id'
end

所有这些技能均显示为按钮,这些按钮可用于启用/禁用该员工的特定技能(通过单击直接插入/删除,不需要额外的提交).

All those skills are displayed as buttons, which are toggles to enable/disable that particular skill on that employee (per click a direct insert/delete, no extra commit needed).

<%= link_to apply_skill_employee_path(employee: @employee, skill_id: skill.id), method: :put, remote: :true do %>
<%= skill.name %></div><% end %>

但是现在,我希望在加载页面时将按钮显示为彩色.如果已经启用该技能,则按钮颜色应为绿色,否则为灰色.从这里开始我的问题: 我的应用程序使用一个单独的选择语句检查每种技能.我为此使用以下代码:

But now, I want to the buttons to be shown colored, when you load the page. If the skill is already enabled, the buttoncolor should be green, else grey. And here begins my problem: My app checks each skill with one separate select statement. I use the following code for this:

<%= link_to apply_skill_employee_path(employee: @employee, skill_id: skill.id), method: :put, remote: :true do %>
  <% if @employee.skills.exists?(skill.id) %>
    <div class="button skill e-true"><%= skill.name %></div>
  <% else %>
    <div class="button skill"><%= skill.name %></div>
  <% end %>

我已经尝试使用包含,但是似乎存在吗?独立检查每个技能.

I have already tried to use includes, but it seems, the exists? checks each skill independently.

这里有人有一个建议,我如何通过使用一个选择就可以解决这个问题?

Does anybody here have a suggestion, how I could solve this, by using one single select?

谢谢,我希望我已经提到了一切,什么是必要的.

Thanks in advance, I hope I have mentioned everything, what is necessary.

我忘了提一下,我通过局部渲染(如果要知道的话). 这是employees_controller中当前使用的@employee var.

Edit 1: I forgot to mention, that i render this through a partial (if that is important to know). And here is the current used @employee var in the employees_controller.

@employee = Employee.find_by(id: params[:id])

推荐答案

尝试 pluck 输入ID,然后检查 include? 需要获取skills

<% skills = @employee.skills.pluck(:id) %>
<%= link_to apply_skill_employee_path(employee: @employee, skill_id: skill.id), method: :put, remote: :true do %>
<% if skills.include?(skill.id) %>
  <div class="button skill e-true"><%= skill.name %></div>
<% else %>
  <div class="button skill"><%= skill.name %></div>
<% end %>

这篇关于Rails,解决此n + 1的技巧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆