Ordering Data by datetime Entry in Rails

I have content_date row in my table that uses the datetime field type. I would like to order my data (content_items) using this field.

models/content_item.rb

class ContentItem < ActiveRecord::Base
  belongs_to :calendar
end

models/calendar.rb

class Calendar < ActiveRecord::Base
    has_many :content_items
end

controllers/content_items_controller.rb

  def index
    @calendar = Calendar.find(params[:calendar_id])
  end

I tried this, but had no success:

controllers/content_items_controller.rb

  def index
    @calendar = Calendar.find(params[:calendar_id])
    @content_item = @calendar.content_items.order(content_date: :desc)
  end


Source: ruby

Leave a Reply