#176 Searchlogic
Searchlogic makes searching models easier than ever with its assortment of named scopes. In this episode I show you how to create simple and advanced searches.
- Download:
- source codeProject Files in Zip (97.1 KB)
- mp4Full Size H.264 Video (22.4 MB)
- m4vSmaller H.264 Video (15 MB)
- webmFull Size VP8 Video (38.7 MB)
- ogvFull Size Theora Video (36.3 MB)
Resources
- Searchlogic
- Hirb
- Episode 111: Advanced Search Form
- Episode 37: Simple Search Form
- Full Episode Source Code
bash
sudo gem install searchlogic
sudo gem install searchlogic
config/environment.rb
config.gem "searchlogic"
config.gem "searchlogic"
script/console
ActiveRecord::Base.logger = Logger.new(STDOUT)
Product.name_like("Video")
Product.name_not_like("Video").price_gt(5).price_lt(200)
Product.name_like_any(["couch", "table"])
Product.name_like_all(["video", "console"])
Product.category_name_like("elect")
Product.search(:category_name_like => "elect", :price_lt => "100")
s = _
s.all
s.name_like("video")
Product.ascend_by_name
ActiveRecord::Base.logger = Logger.new(STDOUT) Product.name_like("Video") Product.name_not_like("Video").price_gt(5).price_lt(200) Product.name_like_any(["couch", "table"]) Product.name_like_all(["video", "console"]) Product.category_name_like("elect") Product.search(:category_name_like => "elect", :price_lt => "100") s = _ s.all s.name_like("video") Product.ascend_by_name
products_controller.rb
@products = Product.name_like_all(params[:search].to_s.split).ascend_by_name
# or
@search = Product.search(params[:search])
@products = @search.all
@products = Product.name_like_all(params[:search].to_s.split).ascend_by_name # or @search = Product.search(params[:search]) @products = @search.all
index.html.erb
<% form_for @search do |f| %>
<p>
<%= f.label :name_like, "Name" %><br />
<%= f.text_field :name_like %>
</p>
<p>
<%= f.label :category_id_equals, "Category" %><br />
<%= f.collection_select :category_id_equals, Category.all, :id, :name, :include_blank => true %>
</p>
<p>
<%= f.label :price_gte, "Price Range" %><br />
<%= f.text_field :price_gte, :size => 8 %> - <%= f.text_field :price_lte, :size => 8 %>
</p>
<p>
<%= f.submit "Submit" %>
</p>
<% end %>
<p>
Sort by:
<%= order @search, :by => :name %> |
<%= order @search, :by => :price %>
</p>
<% form_for @search do |f| %> <p> <%= f.label :name_like, "Name" %><br /> <%= f.text_field :name_like %> </p> <p> <%= f.label :category_id_equals, "Category" %><br /> <%= f.collection_select :category_id_equals, Category.all, :id, :name, :include_blank => true %> </p> <p> <%= f.label :price_gte, "Price Range" %><br /> <%= f.text_field :price_gte, :size => 8 %> - <%= f.text_field :price_lte, :size => 8 %> </p> <p> <%= f.submit "Submit" %> </p> <% end %> <p> Sort by: <%= order @search, :by => :name %> | <%= order @search, :by => :price %> </p>

