# View
<% form_for @search do |f| %>
<%= f.text_field :keyword %>
<%= f.submit 'Search' %>
<% end %>
# Controller
# Using Seachlogic with Will Paginate
class EnrollmentsController < ApplicationController
def index
@search = Enrollment.search(params[:search])
@enrollments, @enrollments_count = @search.paginate(:include => [{:addresses => :address_type}, :course, :gender], :page => params[:page], :per_page => 20), @search.count
end
end
# Model
# params[:search][:keyword] will trigger keyword namedscope
class Enrollment < ActiveRecord::Base
named_scope :keyword, lambda {|keyword|
{ :conditions => [
"first_name like :word or last_name like :word or nric like :word or date_of_birth like :word or mentor like :word or school_approval_status like :word or student_confirmation_status like :word or genders.name like :word or addresses.handphone like :word or courses.name like :word",
{:word => "%#{keyword}%"}
],
:include => [:addresses, :gender, :course]
}
}
end
Tuesday, August 4, 2009
Implementing "google search" like search field using Searchlogic
I just wanna a single search field like how it is in google.com. This search field should then search through all the desired columns and return records with matches.
Avoid multi-level associations using named scope
I had the a "local_address" method in Enrollment which looks for first local address
Calling the "@enrollment.local_address" then leads to very unoptimized queries. To optimized it I had to call "Address.find" directly from the "local_address" method.
class Enrollment < ActiveRecord::Base
has_many :addresses
def local_address
addresses.local.last
end
end
class Address < ActiveRecord::Base
belongs_to :enrollment
belongs_to :address_type
named_scope :local, {
:joins => "LEFT OUTER JOIN address_types ON address_types.id = addresses.address_type_id",
:conditions => "address_types.name = 'Local'"
}
end
Calling the "@enrollment.local_address" then leads to very unoptimized queries. To optimized it I had to call "Address.find" directly from the "local_address" method.
def local_address
- addresses.local.last
+ Address.last :include => :address_type, :conditions => ["address_types.name = 'Local' and enrollment_id = ?", id]
end
Table sort with Searchlogic
Searchlogic comes with a nice rails helper method to assist sorting of your paginated tables
You don't even have to do anything extra to your controller to get some nice sorting action. Cool!
<%= order @search, :by => :created_at, :as => "Created" %>
You don't even have to do anything extra to your controller to get some nice sorting action. Cool!
Usefulness of named scope in remote console
Fancy searching through records in production database in a SSH terminal? You can always query them via good o' SQL but I find that rather cumbersome.
Too long... what about activerecord objects using script/console?
Better but the conditions string can get unweldy. But why not use namedscopes?
Short and sweet! Plus if you use the Searchlogic gem, these namedscopes comes for free! :)
select * from payment_transactions outer left join utility_providers ..... where .....
Too long... what about activerecord objects using script/console?
PaymentTransaction.find(:all, :conditions => [........])
Better but the conditions string can get unweldy. But why not use namedscopes?
PaymentTransaction.vendor('Singtel').amount('30.00').account('123456').date_less_than('2009-08-01')
Short and sweet! Plus if you use the Searchlogic gem, these namedscopes comes for free! :)
Using Searchlogic and Will Paginate
Here's the code to in controller to integrate the searching with binarylogic-searchlogic gem and pagination using mislav-will_paginate gem.
def index
@search = Enrollment.search(params[:search])
@enrollments, @enrollments_count = @search.paginate(:page => params[:page], :per_page => 20), @search.count
end
Sunday, August 2, 2009
Toggling between gnome terminal tabs
Can't believe I haven't used this trick earlier since I use gnome-terminal with multiple tabs EVERYDAY.
Usually I have about 4~5 tabs open for a given gnome-terminal window session. What I did to to go to a particular tab was to hit "CTRL + PAGE UP/PAGE DOWN" to scroll through them. What a silly way because all I need to do was to hit "ALT + tab no." to get to it directly.
Eg.
First tab: ALT + 1
Second tab: ALT + 2
...
Fourth tab: ALT + 4
Usually I have about 4~5 tabs open for a given gnome-terminal window session. What I did to to go to a particular tab was to hit "CTRL + PAGE UP/PAGE DOWN" to scroll through them. What a silly way because all I need to do was to hit "ALT + tab no." to get to it directly.
Eg.
First tab: ALT + 1
Second tab: ALT + 2
...
Fourth tab: ALT + 4
Subscribe to:
Posts (Atom)