我試圖在Texticle搜索的一部分上實現對標籤的搜索。由於texticle不會隨着多個表從同一型號搜索,我結束了創建一個名爲PostSearch新模式,以下Texticle的有關係統範圍內搜索Texticle和ActsAsTaggableOn
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數據庫我創建了這樣一個觀點建議:
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?
嗯......但我不能有一個視圖上的索引我可以嗎?另外,讓它成爲零的原因是有道理的,我想,但你有什麼想法爲什麼它實際上映射到零? – you786 2012-04-26 20:35:51
視圖是臨時表,因此您無法直接對它們編制索引,但是您可以對要從中繪製視圖的基礎表進行索引。我不知道爲什麼它會再次映射到'nil',我建議您與texticle的創建者聯繫以調查「爲什麼」。 – acconrad 2012-04-27 15:22:34