[英]How to select data from multiple model in rails
i have three model project_manager, project_director, and human_resource each has a status Boolean field how can i print some thing in rails if Boolean value of these three model is true.我有三个模型 project_manager、project_director 和 human_resource,每个模型都有一个状态布尔字段,如果这三个模型的布尔值为真,我如何在 rails 中打印一些东西。 currently i am accessing data from model by doing this-目前我正在通过这样做访问模型中的数据-
<% if project_site.project_managers.empty? %>
<td class="pending fi-eye"><%= " Pending" %></td>
<% else %>
<% project_site.project_managers.each do |project_manager| %>
<% if project_manager.status == false %>
<td class="rejected fi-x"><%= ' Rejected' %></td>
<% elsif project_manager.status == true %>
<td class="approved fi-check"><%= " Approved" %></td>
<% end %>
<% end %>
<% end %>
<% if project_site.project_directors.empty? %>
<td class="pending fi-eye"><%= " Pending" %></td>
<% else %>
<% project_site.project_directors.each do |project_director| %>
<% if project_director.status == false %>
<td class="rejected fi-x"><%= ' Rejected' %></td>
<% elsif project_director.status == true %>
<td class="approved fi-check"><%= " Approved" %></td>
<% end %>
<% end %>
<% end %>
<% if project_site.human_resources.empty? %>
<td class="pending fi-eye"><%= " Pending" %></td>
<% else %>
<% project_site.human_resources.each do |human_resource| %>
<% if human_resource.status == false %>
<td class="rejected fi-x"><%= ' Rejected' %></td>
<% elsif human_resource.status == true %>
<td class="approved fi-check"><%= " Approved" %></td>
<% end %>
<% end %>
<% end %>
i want to print approved if all these three model status value is true how can i do that in rails?如果这三个模型状态值都为真,我想打印已批准我怎么能在 Rails 中做到这一点?
Create a helper method and put the following code :创建一个辅助方法并输入以下代码:
def check_resource_status(project_site, resources)
statuses = project_site.send(resources.to_sym).pluck(:status)
statuses.all? ? true : false
end
def status_container(status)
content_tag :div, class: ['sample'] do
status_label = status ? 'Approved' : 'Rejected'
default_class = status ? 'fi-check' : 'fi-x'
status_class = [default_class, status_label.downcase]
concat content_tag(:label, status_label, class: status_class)
end
end
From your view file :从您的视图文件:
status_container(check_resource_status(project_site, 'human_resources'))
status_container(check_resource_status(project_site, 'project_directors'))
status_container(check_resource_status(project_site, 'project_managers'))
If I understood your question correctly, all you need is something like this:如果我正确理解了你的问题,你所需要的就是这样的:
<% if human_resource.status? %>
<td class="approved fi-check"><%= ' Approved' %></td>
<% else %>
<td class="rejected fi-x"><%= ' Rejected' %></td>
<% end %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.