`
juliancg65
  • 浏览: 15871 次
  • 性别: Icon_minigender_1
  • 来自: 上海
最近访客 更多访客>>
社区版块
存档分类
最新评论

#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>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics