1 Reply Latest reply on Aug 1, 2012 12:03 PM by Hamid Seleman

    Order by Date field

    Drew Wesley

      I have a data model that is using property bag and one of the fields contains a date 'mm/dd/yy' field.

       

      #1) Do I need to specify anthing special in the Model's .rb file?:

      class SomeModel include Rhom::PropertyBag

        property :date_field, :date, :overwrite

       

      end

       


       

       

      #2) I am trying to figure out how to do a query and order by date, looking at http://docs.rhomobile.com/rhodes/rhom#advanced-queries what would be my order by syntax to sort by a date field

        • Re: Order by Date field
          Hamid Seleman

          1. No. not that I know of for date datatype field.

          2. I usually have another field (or use the same field) of date converted to integer i.e.: int_date_field = Time.now.to_i

          So, in my model's rb file, I would add:

          ...

          property :int_date_field, :integer

           

          This way the recent the date, the higher its integer value (time included). So, descending order will give me most recent first, while ascending order will be oldest first. And my query would look like this:

           

          @results = Model.find(:all, :orderby => :int_date_field, :orderdir => 'desc') # set 'asc' for ascending order

           

          Hope that helps.