我有以下查询:
query = param.tag_list.join("|")
title = "ts_headline(title, query) AS title"
rank = "ts_rank_cd(tsv, query) AS rank"
Job.select(
[
title, rank, :starts_at, :slug, :job_position_id, :id, :city_id,
:company_office_id
]
).from(
"jobs, to_tsquery('english', '#{query}') AS query"
).where(
"tsv @@ query AND enabled = true"
).with_position.with_city.with_office.order(
"rank DESC, starts_at DESC"
).limit(limit)
with_* 范围只包含职位、城市、办公室。不多。
这会导致 Arel 进行产生错误的查询:
ActiveRecord::StatementInvalid - PG::Error: 错误: 对表“jobs”的 FROM 子句条目的无效引用 LINE 1: ...ER JOIN "job_positions" ON "job_positions"."id" = "jobs"."jo ... ^ 提示:表“jobs”有一个条目,但不能从查询的这一部分引用它。
这很可能与混合隐式和显式 JOINS 的问题有关。另请参阅此错误:对 FROM 子句的无效引用
问题是我不知道如何用 to_tsquery 重写查询以便它工作?
Muchas gracias 提前为任何想法
更新
这是最终由 Arel 生成的确切查询。错误是指第一个 LEFT OUTER JOIN 中的 jobs.job_position_id
SELECT
...
FROM
jobs, to_tsquery('english', 'coffeescript|backbone.js|javascript|node.js') AS query
LEFT OUTER JOIN
"job_positions" ON "job_positions"."id" = "jobs"."job_position_id"
LEFT OUTER JOIN
"cities" ON "cities"."id" = "jobs"."city_id"
LEFT OUTER JOIN
"company_offices" ON "company_offices"."id" = "jobs"."company_office_id"
LEFT OUTER JOIN
"companies" ON "companies"."id" = "company_offices"."company_id"
WHERE
(tsv @@ query AND enabled = true)
ORDER BY
rank DESC, starts_at DESC
LIMIT 9