Rails ActiveRecord Find / Search by Date

后端 未结 3 1354
醉话见心
醉话见心 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:55

    I solved this problem by creating a method in model like below, Say, my model is ticket.rb

     def created_date
       self.created_at.to_date
     end
    

    then I queried like this,

     selected_date = Date.parse(params[:date])
    
     Ticket.all.map{|t| t if t.created_date == selected_date}.compact
    

    This gives me accurate results that matches with the chosen date by the user.

提交回复
热议问题