0

I have a model with different scopes

class Contact
  include Mongoid::Document

  scope :active
  scope :urgent
  scope :no_one_in_charge

In some of my controller, I pull the active scopes

my_controller.rb

def my_action
  @contacts = Contact.active

Now in the view, I'd like to generate many tables with more specific scoping

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 ?] %>
Cyril Duchon-Doris
  • 12,964
  • 9
  • 77
  • 164

1 Answers1

0

You can chain scopes. So your code will be

<h3>Unassigned</h3>

<%= no_one_in_charge_contacts @contacts %>

<h3>Urgent</h3>

<%= urgent_contacts @contacts %>

So, you shouldn't query inside the view, so make 2 helpers for those. Go to the helpers/my_action_helper.rb and write :

def urgent_contacts contact
  contact.urgent
end

def no_one_in_charge_contacts contact
  contact.no_one_in_charge
end
Arup Rakshit
  • 116,827
  • 30
  • 260
  • 317