Method: Multisearch#multisearch

Defined in:
lib/multisearch.rb

#multisearch(searchterm, columnlist) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/multisearch.rb', line 5

def multisearch(searchterm, columnlist)
  @searchterm = searchterm
  @columnlist = columnlist
  @count = columnlist.count
  @results = Hash.new

  if @searchterm
    # get the first coloumn regardless of content length
    @firstkey = @columnlist[0]
    @firstcol = @firstkey.downcase.gsub(' ', '')
    @results[@firstkey] = self.where("#{@firstcol} like ?", "%#{@searchterm}%").to_a

    # iterate through rest of columnlist keeping only keys with values
    if @count > 1

      @columnlist[1..@count].each do |key|
        @col = key.downcase.gsub(' ', '')
        @finds = self.where("#{@col} like ?", "%#{@searchterm}%").to_a
        @results[key] = @finds if @finds.count > 0 
      end

    end

    return @results
  else
    self.find(:all)
  end

end