#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.


sudo gem install searchlogic
# config/environment.rb
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

# 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
<!-- 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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值