Ruby on Rails - search in database based on a query

前端 未结 4 914
灰色年华
灰色年华 2021-01-26 05:54

I have a simple form, where I set up a query that I want to browse, for example panasonic viera. This is on how I search the term in database:

P         


        
4条回答
  •  面向向阳花
    2021-01-26 06:37

    If you're using PostgreSQL, you can use pg_search gem. It's support full text search, with option any_word:

    Setting this attribute to true will perform a search which will return all models containing any word in the search terms.
    

    Example from pg_search:

    class Number < ActiveRecord::Base
      include PgSearch
      pg_search_scope :search_any_word,
                      :against => :text,
                      :using => {
                        :tsearch => {:any_word => true}
                      }
    
      pg_search_scope :search_all_words,
                      :against => :text
    end
    
    one = Number.create! :text => 'one'
    two = Number.create! :text => 'two'
    three = Number.create! :text => 'three'
    
    Number.search_any_word('one two three') # => [one, two, three]
    Number.search_all_words('one two three') # => []
    

提交回复
热议问题