Ruby on railsRanSackDesign by Mr.Huân
Whats RansackRansack is a rewrite of MetaSearch. While it supportsmany of the same features as MetaSearch, its underlying...
Basic Searching• Eq :The eq predicate returns all records where a fieldis exactly equal to a given value.>> Product.search...
Usage ransackRansack can be used in one of two modes:•Simple Mode• The default param key for search params is now :q, inst...
Simple Mode(continue)In your controller:def index@q = Person.search(params[:q])@people = @q.result(:distinct => true)endIn...
Advanced Search• "Advanced" searches (ab)use Rails nested attributes functionality inorder to generate complex queries wit...
Advanced Search(Continue)•  controller action ...def search index render :index end• search_form_for line in the view ...<...
has_many and belongs_toassociationsCan easily use Ransack to search in associated objects.Model:class Product < ActiveReco...
class ProductsController < ApplicationController def index         @search = Product.search(params[:q]    @Products = @sea...
Thank for watching
Upcoming SlideShare
Loading in …5
×

Ransack ruby on rails - HuanND

3,365 views

Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Ransack ruby on rails - HuanND

  1. 1. Ruby on railsRanSackDesign by Mr.Huân
  2. 2. Whats RansackRansack is a rewrite of MetaSearch. While it supportsmany of the same features as MetaSearch, its underlyingimplementation differs greatly from MetaSearch, andbackwards compatibility is not a design goal.Ransack enables the creation of both simple andadvanced search forms against your applications models.
  3. 3. Basic Searching• Eq :The eq predicate returns all records where a fieldis exactly equal to a given value.>> Product.search(:name_eq => "Flux Capacitor").result.to_sql=> "SELECT "products".* FROM "products" WHERE "products"."name" = FluxCapacitor"• Matches• lt• In• Cont• Start• End
  4. 4. Usage ransackRansack can be used in one of two modes:•Simple Mode• The default param key for search params is now :q, insteadof :search.• form_for is now search_form_for and validates that aRansack::Search object is passed to it.• Common ActiveRecord::Relation methods are no longerdelegated by the search object. Instead, you will get your searchresults via a call to Search#result.
  5. 5. Simple Mode(continue)In your controller:def index@q = Person.search(params[:q])@people = @q.result(:distinct => true)endIn your view:<%= search_form_for @q do |f| %><%= f.label :name_cont %> <%= f.text_field :name_cont %><%= f.label :articles_title_start %> <%= f.text_field :articles_title_start %><%= f.submit %><% end %>//View list Products
  6. 6. Advanced Search• "Advanced" searches (ab)use Rails nested attributes functionality inorder to generate complex queries with nested AND/OR groupings,etc. A notable drawback with these searches is that the increasedsize of the parameter string will typically force you to use the HTTPPOST method instead of GET.• routes...resources :products docollection { post :search, to: products#index }endend
  7. 7. Advanced Search(Continue)•  controller action ...def search index render :index end• search_form_for line in the view ...<%= search_form_for @q, :url => search_products_path, :html => {:method => :post} do |f| %>
  8. 8. has_many and belongs_toassociationsCan easily use Ransack to search in associated objects.Model:class Product < ActiveRecord::Base     belongs_to :Categoryend class Category < ActiveRecord::Base      has_many :Productsend 
  9. 9. class ProductsController < ApplicationController def index         @search = Product.search(params[:q]    @Products = @search.result(:distinct => true) end endForm…<%= search_form_for @search do |f| %>   <%= f.label :name_cont %> <%= f.text_field :name_cont %>   <%= f.label :category_name_cont %> <%= f.text_field :categoris_last_name_cont %>   <%= f.submit "search" %> <% end %>
  10. 10. Thank for watching

×