Search on multiple keywords in a single search text field (RAILS)

前端 未结 3 748
你的背包
你的背包 2020-12-18 11:12

I\'m fairly new and playing around with searching databases in Rails. I have a model and database that has a list of names under the \'name\' attribute. I want to be able to

3条回答
  •  萌比男神i
    2020-12-18 11:41

    The code from Łukasz Śliwa works great if you close the name variable with the other % sign.

    The complete code from above working for me. Great post.

    def self.search(search)
    
      if search
        search_length = search.split.length
        find(:all, :conditions => [(['name LIKE ?'] * search_length).join(' AND ')] + search.split.map { |name| "%#{name}%" })
      else
        find(:all)
      end
    
    end
    

提交回复
热议问题