RailsCasts Pro episodes are now free!

Learn more or hide this

impedance's Profile

GitHub User: impedance

Comments by impedance

Avatar

yes, thanks, and if you want to use it with pagination, for me it works like this:

ruby
def self.search(search)
    if search
      where('label LIKE ?', "%#{search}%")
    else
      where('label LIKE ?', "%#{}%")
    end
  end

but later I refactor it to scope

ruby
scope :search, lambda { |search| where("label LIKE ?", "%#{search}%")}
Avatar

Hello, also you can use such scope:
scope :search, lambda { |search| where("label LIKE ?", "%#{search}%")}

as the result query you will have objects which can be paginated also, and it will not crash even if "search" will be nil.