Rails为Mongoid标准添加了范围

时间:2015-04-05 15:06:01

标签: ruby-on-rails mongoid named-scope

我有一个具有不同范围的模型

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

1 个答案:

答案 0 :(得分: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