2015-04-05 56 views
0

我有不同范围的模型Rails的添加范围Mongoid标准

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 ?] %> 
+0

您可以链接范围正确吗? – 2015-04-05 15:07:25

回答

0

你可以chain scopes。所以,你的代码将

<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