Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

am trying to show pictures from database where they been uploaded withing the same week and have like. here is how i do it

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)

and am getting this error

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

what am i doing wrong here

"created_at" IN ('created_at >= ? and created_at <= ?', '2017-02-20 00:00:00', '2017-02-26 23:59:59.999999') is very suspicious in the generated SQL. – pozs Feb 22, 2017 at 11:37 Sorry mate, I am not so keen on SQL queries, but I thought the AND must be capitalized for queries. You might try. May be it works also with 'and' ... => I mean the 'and' in the line that I copied above in the comment...Hä?! – KcUS_unico Feb 22, 2017 at 11:43 try Like.where(created_at: Time.current.all_week) more readable I think, apidock.com/rails/DateAndTime/Calculations/all_week – rails_id Feb 22, 2017 at 11:49 # 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)

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.