Rails ActiveRecord Find / Search by Date

后端 未结 3 1390
醉话见心
醉话见心 2020-12-31 22:22

I am trying to find records by \'created_at\' date - the database column type is \'datetime\' and I am using the UI DatePicker from jQuery

my url look like this: \"

3条回答
  •  北荒
    北荒 (楼主)
    2020-12-31 22:52

    A simple solution I use sometimes is to cast the date(time) field as text on the database rather than parse the string into date on application side. For your case that would be:

    where('CAST(created_at AS text) LIKE ?', params[:selected_date])
    

    Might not be the most effective on the database (depending on the context you use it in) but saves a lot of pita on the application side.

提交回复
热议问题