You can use send method for calling scopes as followed:
<%- %w(scope1 scope2).each do |scope|
<h1><%= scope %></h1>
<%- Model.send(scope.to_sym).each do |record| %>
<%= record.attribute%>
<%- end%>
<%- end %>
However looping more than a scope may cause huge data to be rendered on a single page. I would suggest avoiding this.
Following is an way to you can avoid looping multiple scopes on a single page:
- Keeping a select list on the page may help you to display only selective data. For example say you are having model User as :
class User < ActiveRecord::Base
scope :active, -> { where active: true }
scope :inactive, -> { where active: false }
end
Then your select list will be :
<%= select_tag(:scope_name, options_for_select([['All', 'all'], ['Active Users', 'active'], ['InActive Users', 'inactive'],...])) %>
Above select list will have options of All, Active Users, InActive Users with values as actual scope names that you are having in your model.
With JS/JQuery you can write coding to send a get request to the controller which you were using for displaying data. So when user will be selecting any option the page will get reloaded. It can be done using
following code:
$(function (){
$('#scope_name').change(function (){
window.location.replace('/users?scope_name='+ $(this ).val());
})
}
With above coding you will be getting scope_name parameter in params in your controller.
Use this params to retrieve records in controller only as follows:
In my controller action:
def index
@scope_name = if %w(all, active, inactive).includes?(params[:scope_name]) # taking care that proper scope name is passed here
params[:scope_name ]
else
'all'
end
@records = User.send(@scope_name .to_sym)
end
end
Now this @records can be used on the template(views/users/index.html.erb) for displaying the record attributes:
<h1><%= @scope_name.titleize %></h1>
<%- @records.each do |record| %>
<%= record.first_name%>
<%- end%>
This way you will be having selective data on a page and you will not have to fire any query from view.