我有一个具有不同范围的模型
class Contact
include Mongoid::Document
scope :active
scope :urgent
scope :no_one_in_charge
在我的一些控制器中,我拉动活动范围
my_controller.rb
def my_action
@contacts = Contact.active
现在,在视图中,我想生成许多具有更具体作用域的表
my_action.html.erb
<h3>Unassigned</h3>
<%= @contacts.[how Do I add the :no_one_in_charge scope ?] %>
<h3>Urgent</h3>
<%= @contacts.[how Do I add the :urgent scope ?] %>
您可以链接作用域。所以你的代码将是
<h3>Unassigned</h3>
<%= no_one_in_charge_contacts @contacts %>
<h3>Urgent</h3>
<%= urgent_contacts @contacts %>
因此,您不应该在视图中进行查询,因此为这些查询创建2个助手。转到helpers/my_action_helper.rb
并写入:
def urgent_contacts contact
contact.urgent
end
def no_one_in_charge_contacts contact
contact.no_one_in_charge
end