Ruby on Rails - Railscast #213 Calendar sort by datetime not date


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


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 <a href="http://apidock.com/rails/ActiveRecord/QueryMethods/order" rel="nofollow">http://apidock.com/rails/ActiveRecord/QueryMethods/order</a>


  • Importing SCORM compliant Lesson (imsmanifest.xml)
  • push into deeply nested array
  • Find a number that is between two values and return field corresponding to lower value
  • Optimize access query with nested IIF's
  • how can I get other db instance in nodejs 3.0 mongo-client?
  • Can't get key to display in Django template
  • Renaming a file extension without specifying
  • Descriptor 'date' requires a 'datetime.datetime' object but received a 'uni
  • How to use : function in H2O ddply, R?
  • Jenkins: could not create Android emulator failed to parse AVD config file
  • Struts 2 TextField Tag with an attribute and no value
  • Android Google Maps API v2 start navigation
  • Grails calculated field in SQL
  • why xml file does not aligned properly after append the string in beginning and end of the file usin
  • Date Conversion from yyyy-mm-dd to dd-mm-yyyy
  • Sencha Touch 2.0 Controller refs attribute not working?
  • Dialing with Intent.ACTION_CALL stopps at # in phone number
  • Is there a javascript serializer for JSON.Net?
  • PHPUnit_Framework_TestCase class is not available. Fix… - Makegood , Eclipse
  • Java Scanner input dilemma. Automatically inputs without allowing user to type
  • Illegal mix of collations for operation for date/time comparison
  • Master page gives error
  • How to add date and time under each post in guestbook in google app engine
  • JSON with duplicate key names losing information when parsed
  • Release, debug version and Authorization Google?
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Return words with double consecutive letters
  • bootstrap to use multiple ng-app
  • PHP: When would you need the self:: keyword?
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • Proper way to use connect-multiparty with express.js?
  • Acquiring multiple attributes from .xml file in c#
  • Hits per day in Google Big Query
  • Can't mass-assign protected attributes when import data from csv file
  • sending mail using smtp is too slow
  • reshape alternating columns in less time and using less memory
  • Binding checkboxes to object values in AngularJs
  • Conditional In-Line CSS for IE and Others?
  • How can I use threading to 'tick' a timer to be accessed by other threads?
  • How to load view controller without button in storyboard?