我有一个使用自定义 sql 字符串(带有 postgres 数据库)的 Rails 应用程序,如下所示。该字符串工作正常。我正在尝试在查询中添加时间条件,以便仅从过去 7 天内创建的 answer_votes、best_answers 和贡献中检索记录。因此,我得到今天的日期并减去 7 天
date = Date.today - 7
并将下面的 sql 字符串中的语句替换为类似的内容
(select Coalesce(sum(value),0) from answer_votes where (answer_votes.user_id = users.id) AND (answer_votes.created_at <= #{date}))
但是,它给了我几个错误
PG::Error: ERROR: operator does not exist: timestamp without time zone <= integer
你能告诉我正确的方法吗?请注意,虽然我只展示了我对 answer_votes 的尝试,但我将尝试对 best_answers 和贡献做同样的事情,但是,解决方案将是相同的(我假设)。
sql_string = "with cte_scoring as (
select
users.id, users.name, users.email,
(select Coalesce(sum(value),0) from answer_votes where answer_votes.user_id = users.id) +
(select Coalesce(sum(value),0) from best_answers where best_answers.user_id = users.id) +
(select Coalesce(sum(value),0) from contributions where contributions.user_id = users.id) total_score
from
users join
contacts on (contacts.user_id = users.id)
where
users.lawyer = 'true')
select id,
name,
email,
total_score
from cte_scoring
order by total_score desc
limit 2 "