Ruby on Rails Monday, May 5, 2014

Hello all,

I am wondering how I can loop through the scopes defined in a model. As an example, if I want to create an erb template that lists the content of of a model separated by scope with the header of that content being the scope name, I would like to be able to do this without having to create a specific loop for each scope.

Instead of something like

<h1>Scope1</h1>
<% Model.scope1.each do |record| %>
 
<%= record.attribute%>
<% end %>

<h1>Scope2</h1>
<% Model.scope2.each do |record| %>
  
<%= record.attribute%>
<% end %>

I would like to do something like

%w(scope1 scope2).each do |scope|
  <h1><%= scope %></h1>
  <% Model.scope.each do |record| %>
    
<%= record.attribute%>
  <% end%>
<% end %>

Which hopefully would give me the same output with less code. Does anyone have any ideas on how I might be able to accomplish this?

Thanks

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/ddbcb0e7-3b46-4b98-807f-223e0cdd086f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment