Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter records based on date in Rails

I am trying to filer records based on date from created_at.

For this I am passing from_date and to_date from date-picker in Date Format (e.g. "%y/%m/%d").

Please help me with how to write query in the controller to fetch the records between dates.

@users = User.where(['created_at = ? and created_at = ?',from_date,to_date])

Thanks.

like image 584
lamrin Avatar asked Nov 25 '25 09:11

lamrin


1 Answers

You can use the range syntax, looks cleaner:

@users = User.where(:created_at => start_date.to_time..end_date.to_time)
like image 155
Pedro Nascimento Avatar answered Nov 28 '25 15:11

Pedro Nascimento