我有一个 Web 服务,允许客户搜索带有查询参数的文章。如果只包含一个参数,它可以正常工作,但如果我结合search_query
和 category
. 这基于找到for_category的Comfortable_Mexican_Sofa。即使我删除该语句,我也会收到此错误。order
错误
PG::InvalidColumnReference: 错误:对于 SELECT DISTINCT,ORDER BY 表达式必须出现在选择列表第 1 行中:...ms_categories"."label" = 'Company News' ORDER BY pg_search_... ^ : SELECT DISTINCT "comfy_cms_pages"。 *从“comfy_cms_pages”在“comfy_cms_categorizations”中加入“comfy_cms_categorizations”。“categorized_id”=“comfy_cms_pages”。“id”和“comfy_cms_categorizations”。“categorized_type”=1美元在“comfy_cms_categories”中加入“comfy_cms_categories”。 "comfy_cms_categorizations"."category_id" INNER JOIN (SELECT "comfy_cms_pages"."id" AS pg_search_id,(ts_rank((to_tsvector('simple', coalesce("comfy_cms_pages"."content_cache"::text, '')) || to_tsvector('simple', coalesce("comfy_cms_pages"."label"::text, '' ))), (to_tsquery('simple', ''' ' || 'austin' || ' ''' || ':')), 0)) AS rank FROM "comfy_cms_pages" WHERE (((to_tsvector('simple', coalesce("comfy_cms_pages"."content_cache"::text, '')) || to_tsvector('simple', coalesce( "comfy_cms_pages"."label"::text, ''))) @@ (to_tsquery('simple', ''' ' || 'austin' || ' ''' || ': '))))) pg_search_comfy_cms_pages ON "comfy_cms_pages"."id" = pg_search_comfy_cms_pages.pg_search_id WHERE (layout_id = '1' AND is_published = 't') AND "comfy_cms_categories"."label" = 'Company News' ORDER BY pg_search_comfy_cms_pages.rank" DESC, "comfy_cms_categories" ."id" ASC, "comfy_cms_pages"."created_at" DESC
应用程序/模型/article.rb
class Article < Comfy::Cms::Page
cms_is_categorized
include PgSearch
pg_search_scope :search_by_keywords, against: [:content_cache, :label], using: { tsearch: { any_word: true, prefix: true } }
应用程序/命令/search_articles_command.rb
class SearchArticlesCommand
def initialize(params = {})
@since = params[:since_date]
@keys = params[:search_query]
@category = params[:category]
end
def execute
Article.unscoped do
query = if @since.present?
Article.article.since_date(@since)
else
Article.published_article
end
query = query.for_category(@category) if @category.present?
query = query.search_by_keywords(@keys) if @keys.present?
query.where('').order(created_at: :desc)
end
end
end
舒适的墨西哥沙发/lib/comfortable_mexican_sofa/extensions/is_categorized.rb
module ComfortableMexicanSofa::IsCategorized
def self.included(base)
base.send :extend, ClassMethods
end
module ClassMethods
def cms_is_categorized
include ComfortableMexicanSofa::IsCategorized::InstanceMethods
has_many :categorizations,
:as => :categorized,
:class_name => 'Comfy::Cms::Categorization',
:dependent => :destroy
has_many :categories,
:through => :categorizations,
:class_name => 'Comfy::Cms::Category'
attr_accessor :category_ids
after_save :sync_categories
scope :for_category, lambda { |*categories|
if (categories = [categories].flatten.compact).present?
self.distinct.
joins(:categorizations => :category).
where('comfy_cms_categories.label' => categories)
end
}
end
end
module InstanceMethods
def sync_categories
(self.category_ids || {}).each do |category_id, flag|
case flag.to_i
when 1
if category = Comfy::Cms::Category.find_by_id(category_id)
category.categorizations.create(:categorized => self)
end
when 0
self.categorizations.where(:category_id => category_id).destroy_all
end
end
end
end
end
ActiveRecord::Base.send :include, ComfortableMexicanSofa::IsCategorized
更新错误
PG::SyntaxError: ERROR: syntax error at or near "."
LINE 4: ...e = 'Class' AND categorized_id = 'comfy_cms_pages'.'id' AND ...
^
: SELECT "comfy_cms_pages".* FROM "comfy_cms_pages" INNER JOIN (SELECT "comfy_cms_pages"."id" AS pg_search_id, (ts_rank((to_tsvector('simple', coalesce("comfy_cms_pages"."content_cache"::text, '')) || to_tsvector('simple', coalesce("comfy_cms_pages"."label"::text, ''))), (to_tsquery('simple', ''' ' || 'austin' || ' ''' || ':*')), 0)) AS rank FROM "comfy_cms_pages" WHERE (((to_tsvector('simple', coalesce("comfy_cms_pages"."content_cache"::text, '')) || to_tsvector('simple', coalesce("comfy_cms_pages"."label"::text, ''))) @@ (to_tsquery('simple', ''' ' || 'austin' || ' ''' || ':*'))))) pg_search_comfy_cms_pages ON "comfy_cms_pages"."id" = pg_search_comfy_cms_pages.pg_search_id WHERE "comfy_cms_pages"."layout_id" = $1 AND "comfy_cms_pages"."is_published" = $2 AND (
EXISTS (
SELECT 1 FROM categorizations
WHERE categorized_type = 'Class' AND categorized_id = 'comfy_cms_pages'.'id' AND category_id IN (2)
)) ORDER BY pg_search_comfy_cms_pages.rank DESC, "comfy_cms_pages"."id" ASC
工作解决方案,但不是范围,并且必须小心调用它的顺序
def self.for_category(_category)
Comfy::Cms::Categorization.includes(:category).references(:category).select(:categorized).pluck(:categorized_id)
find(ids)
end