我有这两个模型:
class Comment < ActiveRecord::Base
belongs_to :post
end
class Post < ActiveRecord::Base
include PgSearch
has_many :comments, dependent: destroy
pg_search_scope :search_tsv, against: [:name],
using: { tsearch: {
tsvector_column: 'tsv',
dictionary: 'english',
prefix: true, any_word: true
}
}
scope :full_search, ->(q) {
select('DISTINCT ON (comments.post_id) comments.*, posts.name as post_name, posts.id as post_id')
.order('comments.post_id, comments.created_at DESC')
.search_tsv(q)
.joins('LEFT JOIN comments on comments.post_id = posts.id')
}
end
如您所见,我尝试在我的 Post 模型上实现全文搜索。我构建了 TSVECTOR 列并触发来更新它,如果我只使用search_tsv
范围,所有的工作就像一个魅力。
但我希望搜索结果中的每篇文章都能检索最后添加的评论。为此,我建立了full_search
范围。
当我尝试使用此范围时,生成的 SQL 查询如下所示:
> Post.full_search('My post name').to_sql
> SELECT DISTINCT ON (comments.post_id) comments.*,
posts.name as post_name, potst.id as post_id
FROM "posts"
INNER JOIN (SELECT "posts"."id" AS pg_search_id, (ts_rank(("posts"."tsv"), (to_tsquery('english', ''' ' || 'My' || ' ''' || ':*') || to_tsquery('english', ''' ' || 'post' || ' ''' || ':*') || to_tsquery('english', ''' ' || 'name' || ' ''' || ':*')), 0)) AS rank FROM "posts" WHERE ((("posts"."tsv") @@ (to_tsquery('english', ''' ' || 'My' || ' ''' || ':*') || to_tsquery('english', ''' ' || 'post' || ' ''' || ':*') || to_tsquery('english', ''' ' || 'name' || ' ''' || ':*'))))) AS pg_search_00699f600cf5a0ff57479a ON "posts"."id" = pg_search_00699f600cf5a0ff57479a.pg_search_id
LEFT JOIN comments on comments.post_id = posts.id
ORDER BY comments.post_id, comments.created_at DESC, pg_search_00699f600cf5a0ff57479a.rank DESC, "posts"."id" ASC
这对我来说看起来不错。但是当我尝试在我的搜索控制器中使用这个范围时,结果很奇怪......如果我这样做:
posts = Post.full_search('My post name')
k = posts.first.comments
...
它生成这个 SQL 查询:
SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = $1 ORDER BY created_at DESC, rank DESC, id DESC [["post_id", 7]]
结果是一个空数组:(。
我无法理解我做错了什么[显然我在这里做了一些愚蠢的事情:(]。
你能帮我解决这个问题吗?