2
我为我的“资产”模型提供了一个搜索页面,除了搜索“serial_no”字段特定的任何内容外,一切工作都正常。pg_search不返回单个字段的结果
Asset.rb
# == Schema Information
#
# Table name: assets
#
# id :integer not null, primary key
# asset_description :string(255)
# asset_type :string(255)
# status :string(255)
# serial_no :string(255)
# user_id :integer
# cost :decimal(,)
# barcode :string(255)
# comment :string(255)
# date_purchased :date
# created_at :datetime not null
# updated_at :datetime not null
#
class Asset < ActiveRecord::Base
attr_accessible :asset_description, :asset_type, :comment,
:date_purchased, :serial_no, :status, :user_id, :cost, :barcode
belongs_to :user
validates :user_id, presence: true
validates :asset_description, presence: true
validates :asset_type, presence: true
validates :serial_no, presence: true, uniqueness: true
validates :status, presence: true
validates :comment, length: { maximum: 150 }
include PgSearch
pg_search_scope :search, against: [:asset_description, :status, :asset_type,
:comment, :cost, :serial_no, :date_purchased],
using: {tsearch: { dictionary: "english" }},
associated_against: { user: :name }
#older assets first
default_scope order: 'assets.created_at ASC'
def self.text_search(query)
if query.present?
search(query)
else
scoped
end
end
end
例如,我与serial_no资产:Z21313412A但是当我搜索 “Z” 不返回任何结果。我一直试图解决这个问题几个星期,但没有运气。
谢谢,我一定是在扫描这4或5倍,仍然错过了! – RyanMacG
很高兴帮助... – MurifoX
@MurifoX当对网址进行搜索时,此解决方案无效。例如,如果url字段为'www.google.com',如果我使用'google'进行搜索,则不会返回记录。任何想法? – Abhi