rails PG :: InvalidDatetimeFormat:错误:类型为timestamp的输入语法无效

时间:2017-02-22 11:15:52

标签: ruby-on-rails postgresql ruby-on-rails-5

我正在尝试显示数据库中的图片,这些图片是在同一周上传的,并且喜欢。这是我怎么做的

def trending_wide
  # binding.pry
  date = DateTime.now.utc
  likes = Like.where('created_at >= ? and created_at <= ?', date.beginning_of_week, date.utc.end_of_week).select(:selfy_id)
  time = 'created_at >= ? and created_at <= ?', date.beginning_of_week, date.utc.end_of_week
  top = Selfy.where(id: likes, created_at: time)
  top.order("COALESCE(likes_count, 0) DESC").limit(6)
end

我收到此错误

PG::InvalidDatetimeFormat: ERROR:  invalid input syntax for type timestamp: "created_at >= ? and created_at <= ?"
LINE 1: ...23:59:59.999999')) AND "selfies"."created_at" IN ('created_a...
                                                             ^
: SELECT COUNT(count_column) FROM (SELECT  1 AS count_column FROM "selfies" WHERE "selfies"."id" IN (SELECT "likes"."selfy_id" FROM "likes" WHERE (created_at >= '2017-02-20 00:00:00' and created_at <= '2017-02-26 23:59:59.999999')) AND "selfies"."created_at" IN ('created_at >= ? and created_at <= ?', '2017-02-20 00:00:00', '2017-02-26 23:59:59.999999') LIMIT $1) subquery_for_count

我在这里做错了什么

1 个答案:

答案 0 :(得分:0)

根据上面给出的答案,我可以用这个来解决它

 def trending_wide
  # binding.pry
  date = DateTime.now.utc
  likes = Like.where('created_at >= ? and created_at <= ?', date.beginning_of_week, date.utc.end_of_week).select(:selfy_id)
  top = Selfy.where(id: likes, created_at: Time.current.all_week)
  top.order("COALESCE(likes_count, 0) DESC").limit(6)
end