gpt4 book ai didi

ruby-on-rails-3 - pg_search 不返回单个字段的结果

转载 作者:行者123 更新时间:2023-12-04 18:18:55 24 4
gpt4 key购买 nike

我的“ Assets ”模型有一个搜索页面,除了搜索特定于“serial_no”字段的任何内容之外,一切正常。

Assets .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

例如,我有一个序列号为 Z21313412A 的 Assets ,但是当我搜索“Z”时,没有返回任何结果。我一直试图解决这个问题几个星期,但没有运气。

最佳答案

根据documentation :

PostgreSQL’s full text search matches on whole words by default. 
If you want to search for partial words, however, you can set :prefix to true.
Since this is a :tsearch-specific option, you should pass it to :tsearch
directly, as shown in the following example.

也许添加 :prefix你的配置解决了这个问题。
来自 Github 页面的示例:
pg_search_scope :whose_name_starts_with,
:against => :name,
:using => {
:tsearch => {:prefix => true}
}

关于ruby-on-rails-3 - pg_search 不返回单个字段的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11117389/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com