Reputation: 6622
I set a search_start_date and a search_end_date and then I want to find records which fall into this time range. But I get nothing back using the code below. What should I change to make this work?
search_start_date = "01-01-2013"
search_end_date = "01-12-2013"
@activities = @employee.activities
projects = @activities.where("start_date >= ? and end_date =< ?", search_start_date, search_end_date)
Edit: I now use search_start_date = DateTime.new(2013, 1, 1)
, which seems to work.
Upvotes: 0
Views: 259
Reputation: 5111
Just modifying the @derekyau solution:-
search_start_date = "01-01-2013".to_date
search_end_date = "01-12-2013".to_date
projects = @activities.where("start_date >= ? and end_date =< ?", search_start_date, search_end_date)
Edit: I now use search_start_date = DateTime.new(2013, 1, 1)
, which seems to work.
Upvotes: 0
Reputation: 2716
The Dates you are using are ambiguous. It is not clear if it's MM-DD-YYYY or DD-MM-YYYY. Depending on the region where you live, both could be correct. Use the default ruby unambiguous formatting YYYY-MM-DD or even better, use actual date objects.
You can also parse your strings to dates like in the example below. I also corrected the second compare operator to make it more concise. I'm unsure if both would have worked anyway. And don't forget the .all
in case you didn't just leave it out of your querstion on purpose.
search_start_date = Date.strptime("01-01-2013", "%d-%m-%Y")
search_end_date = Date.strptime("01-12-2013", "%d-%m-%Y")
projects = @activities.where("start_date >= ? and end_date <= ?", search_start_date, search_end_date).all
Upvotes: 1
Reputation: 2946
You will need to feed "Date" or "Time" objects into the ActiveRecord query, try something like this:
search_start_date = Date.parse("01-01-2013")
search_end_date = Date.parse("01-12-2013")
projects = @activities.where("start_date >= ? and end_date =< ?", search_start_date, search_end_date)
Upvotes: 1