Ruby on Rails - search in database based on a query

前端 未结 4 905
灰色年华
灰色年华 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:30

    How about via ARel

    def self.search(query)
      words      = query.split(/\s+/)
      table      = self.arel_table
      predicates = []
    
      words.each do |word|
        predicates << table[:name].matches("%#{word}%")
      end
    
      if predicates.size > 1
        first = predicates.shift
        conditions = Arel::Nodes::Grouping.new(predicates.inject(first) {|memo, expr| Arel::Nodes::Or.new(memo, expr)})
      else
        conditions = predicates.first
      end
    
      where(conditions).to_a
    end
    
    0 讨论(0)
  • 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') # => []
    
    0 讨论(0)
  • 2021-01-26 06:45

    This isn't working?

     WHERE name LIKE "panasonic" OR name LIKE "viera"
    
    0 讨论(0)
  • 2021-01-26 06:47

    One solution would be to break up your query into individual terms and build a set of database queries connected by OR.

    terms = params[:q].split
    query = terms.map { |term| "name like '%#{term}%'" }.join(" OR ")
    Product.where(query).order('price')
    
    0 讨论(0)
提交回复
热议问题