2015-04-05 74 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