E
E
eldar_web2016-08-20 14:27:45
Ruby on Rails
eldar_web, 2016-08-20 14:27:45

How to sort by date on table elements in Ruby on Rails?

Suppose there is a Period model with columns (of type string) period_start and period_finish.
There are such records:
1)period_start: 01/01/2014, period_start 12/01/2014
2)period_start: 01/01/2016, period_start 12/01/2016 3)period_start: 01/01/2015 ,
period_start 12/01/2015
where period_finish is newer even if period_start = 01/01/1718) came out at the beginning, and old ones at the end?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
Chronic 86, 2016-08-20
@eldar_web

@sort = Model.all.sort_by { |d| -(d.period_finish).to_i}
Don't store in a string what can be stored as numbers. Work with data types as you need at the level of controllers, views.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question