[英]Texticle and ActsAsTaggableOn
我正在嘗試實現對標簽的搜索,作為Texticle搜索的一部分。 由於texticle不會從同一個模型中搜索多個表,因此我遵循Texticle關於系統范圍搜索的建議,最終創建了一個名為PostSearch的新模型。
class PostSearch < ActiveRecord::Base
# We want to reference various models
belongs_to :searchable, :polymorphic => true
# Wish we could eliminate n + 1 query problems,
# but we can't include polymorphic models when
# using scopes to search in Rails 3
# default_scope :include => :searchable
# Search.new('query') to search for 'query'
# across searchable models
def self.new(query)
debugger
query = query.to_s
return [] if query.empty?
self.search(query).map!(&:searchable)
#self.search(query) <-- this works, not sure why I shouldn't use it.
end
# Search records are never modified
def readonly?; true; end
# Our view doesn't have primary keys, so we need
# to be explicit about how to tell different search
# results apart; without this, we can't use :include
# to avoid n + 1 query problems
def hash
id.hash
end
def eql?(result)
id == result.id
end
end
在我的Postgres DB中,我創建了這樣的視圖:
CREATE VIEW post_searches AS
SELECT posts.id, posts.name, string_agg(tags.name, ', ') AS tags
FROM posts
LEFT JOIN taggings ON taggings.taggable_id = posts.id
LEFT JOIN tags ON taggings.tag_id = tags.id
GROUP BY posts.id;
這使我可以獲得如下信息:
SELECT * FROM post_searches
id | name | tags
1 Intro introduction, funny, nice
因此,似乎一切都很好。 不幸的是,調用PostSearch.new(“ funny”)返回[nil](NOT [])。 瀏覽Texticle源代碼,似乎在PostSearch.new中的這一行
self.search(query).map!(&:searchable)
使用某種searchable_columns方法映射字段,這是否“不正確”? 結果為零。
另一方面,除非在文本SQL查詢中將其從文本類型轉換為varchar類型,否則不會在標簽字段中搜索標簽字段。
因此,總而言之:為什么找到對象時將其映射為nil?
和
為什么texticle會忽略我的標簽字段,除非它是varchar?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.