Non-Functional Use of Named Scope

Tagged with Rails named_scope rest

Language: Ruby

View as text

# Model
class Searchable
   cattr_accessor :conditions
   named_scope :filtered, lambda{{:conditions => conditions}}
end

# Controller
def index
   if params[:conditions]
      Searchable.conditions = params[:conditions]
      @searchables = Searchable.filtered
   else
      @searchables = Searchable.all
   end
end

# View
<% for searchable in @searchables %>
   <%= Searchable.content %>
<% end %>

<%= link_to "Show only cool items", :action => :index, :conditions => ['cool= ?',true] %>
Original snippet written by Kevin Gisi
Last updated at 20:32 PM on Aug 08, 2008

SnippetStash costs money to host and develop. The service is free for everyone to use
but if you found it useful please consider making a small donation.