Issue in date conversion in ruby -


in @query putting date in mm-dd-yyyy format. e.g., 01-22-2016. want search dates messages table date how can do. had added query:

message.includes(:user)   .select('messages.*, users.name')   .where(     "users.name ilike ? or messages.to ilike ? or messages.created_at.srtftime(\"%y-%m-%d\") = ?",     "%#{@query}%",     "%#{@query}%",     date.strptime(@query, "%m-%d-%y")   ) 

but getting issue that:

activerecord::statementinvalid: pg::undefinedcolumn: error:  column "%y-%m-%d" not exist 

i had added query as:

message.includes(:user)   .select('messages.*, users.name')   .where(     "users.name ilike ? or messages.to ilike ? or messages.created_at.srtftime("%y-%m-%d") = ?",     "%#{@query}%",     "%#{@query}%",     date.strptime(@query, "%m-%d-%y")   ) 

but in error coming as: unknown type of %string

from comment on @amadan's answer:

if don't convert message.created_at getting blank result.

message.includes(:user)   .select('messages.*, users.name')   .where(     "users.name ilike ? or messages.to ilike ? or messages.created_at = ?",     "%#{@query}%",     "%#{@query}%",     date.strptime(@query, "%m-%d-%y") ) 

this produce query (among others) condition this:

messages.created_at = '2016-01-29 00:00:00' 

it should apparent why won't work. messages.created_at timestamp, , unless record happens have been created @ midnight, equality comparison going fail.

what need compare date part of messages.created_at date query. date part of timestamp in postgresql, can use either date() function or ::date suffix:

message.includes(:user)   .select('messages.*, users.name')   .where(     "messages.created_at::date = ?",     date.strptime(@query, "%m-%d-%y") ) 

Comments