Lauree Roberts
2014-May-07 04:46 UTC
Re: How to loop through model scopes to generate a listing?
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 %> Thanks, Lauree Ruby on Rails Developer <http://www.allerin.com> Allerin Technologies On Monday, May 5, 2014 6:57:13 PM UTC+5:30, Vell wrote:> > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/8e9a2094-60a6-46f1-a4f0-a1fb8fffda90%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.