[英]How can make a decision based on multiple models in Rails?
i have three model project_site, project_manager and human_resource.我有三个模型 project_site、project_manager 和 human_resource。 each model has status Boolean attribute i want to print "approve" only if all status==true but want to print "reject" if any one of the status is set to false.
每个模型都有状态布尔属性,我只想在所有状态==真时打印“批准”,但如果任何一个状态设置为假,我想打印“拒绝”。 each status has nil value by default.
默认情况下,每个状态都有 nil 值。
<% if project_site.human_resources.empty? %>
<td class="pending fi-eye"><%= " Pending" %></td>
<% elsif %>
<% 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 %>
<% elsif %>
<% project_site.project_directors.each do |project_director| %>
<% if project_director.status == false %>
<td class="rejected fi-x"><%= ' Rejected' %></td>
<% end %>
<% end %>
<% elsif %>
<% project_site.project_managers.each do |project_manager| %>
<% if project_manager.status == false %>
<td class="rejected fi-x"><%= ' Rejected' %></td>
<% end %>
<% end %>
<% end %>
Try something like:尝试类似:
is_approved = project_site.human_resources.all?{|e| e.status } # or .all?(&:status) will check if status true for all records
you can even simplify你甚至可以简化
all_approved = (project_site.project_managers + project_site.human_resources + project_site.project_directors).all?(&:status)
it doesn't look perfect and I suggest to move it to the model.它看起来并不完美,我建议将其移至模型中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.