【问题标题】:Rails add scope to Mongoid criteriaRails 为 Mongoid 标准增加了范围
【发布时间】:2015-04-05 23:12:00
【问题描述】:

我有一个不同范围的模型

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

【问题讨论】:

  • 你可以链接范围吗?

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


【解决方案1】:

您可以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

【讨论】:

    猜你喜欢
    • 2011-07-28
    • 1970-01-01
    • 2011-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多