0

I am building an app that involves a calendar. I found that the railscast for building a calendar although dated proved to be very helpful. I have everything up and running I am just trying to allow for the contents on each date to be organized by the datetime and not just the date so they appear in the right chronological order.

My current controller looks like this:

def index
  @lessons = Lesson.all
  @lesson_by_date = @lessons.group_by { |i| i.lesson_date.to_date }
  @date = params[:date] ? Date.parse(params[:date]) : Date.today
end

Any help would be greatly appreciated. Thanks

PilotLite
  • 11
  • 3

1 Answers1

0

I'm going off the assumption your question is how do I organize my @lessons by datetime. There are a few different scenarios I'll mention. I'm a bit limited since I don't know from your post how you've organized your schema for the table, but I think the controller gives me a ballpark idea.

1) You should replace:

@lesson = Lesson.all
@lesson_by_date = @lessons.group_by { |i| i.lesson_date.to_date }

with something that's quicker:

@lesson_by_date = Lesson.order('lesson_date')

2) If you're wondering why your current code isn't working it's because your group_by block is trying to convert i.lesson_date to a date IE to_date. Instead you may want to try to_datetime.

3) I would review http://apidock.com/rails/ActiveRecord/QueryMethods/order

Stephen
  • 337
  • 2
  • 7