Heroku Postgres错误:列不存在

时间:2012-05-07 20:07:00

标签: ruby-on-rails postgresql activerecord heroku

我部署到heroku(运行PG)后出现以下错误,虽然它在本地运行正常(运行sqlite3)

ActionView::Template::Error (PGError: ERROR:  column "calendar_day" does not exist
LINE 1: ...ated_at > '2012-04-09 19:39:54.787077') AND (date(calendar_d...
                                                         ^
SELECT  date(created_at) as calendar_day, count(*) as total_checkins FROM "checkins"  WHERE (user_id = 1 AND group_id = 1 AND created_at > '2012-04-09 19:39:54.787077') AND (date(calendar_day) = '2012-05-07') GROUP BY date(created_at) LIMIT 1):
...
app/helpers/groups_helper.rb:32:in `checkin_day'

请注意,“calendar_day”不是持久表上的实际列,而是我使用AS语句动态定义的列(不确定这在技术上是什么)。相关的帮助函数如下所示。基本上,在页面顶部我调用calendar_view,稍后我将结果传递给checkin_day作为checkins参数。在这次checkin_day调用中,我遇到了错误。知道为什么这在本地工作正常但不在heroku中工作吗?

def calendar_view(member, timeperiod)
  Checkin.select("date(created_at) as calendar_day, count(*) as total_checkins").where("user_id = ? AND group_id = ? AND created_at > ?", member.member.id, member.group.id, Time.now - timeperiod.days).group("date(created_at)")
end

def checkin_day(checkins, day)
  ch = checkins.where("date(calendar_day) = ?", day).first
  return ch.total_checkins if ch
  return 0
end

1 个答案:

答案 0 :(得分:2)

在评估WHERE子句(选择)期间,您在查询的SELECT部分​​定义的内容(投影)不可用。你必须在那里再次定义它:

def checkin_day(checkins, day)
  ch = checkins.where("date(created_at) = ?", day).first
  return ch.total_checkins if ch
  return 0
end