Rails, how to sort the array of active records by created_at
You can always use the order method:
@total.order! 'created_at DESC'
And if you need descending order just use reverse
@total.sort_by(&:created_at).reverse
You can sort an array by using sort_by.
In your case, you could do the following:
@total.sort_by(&:created_at)
Update:
- Remove @all << @find_user and @all_entry << @find_referal_user
- Why? Because otherwise you will have an array with multiple arrays
- Replace @total with: @total = @find_user + @find_referal_user
- Why? Because @total will now only consists of one merged array with all objects, ready to get sorted through .sort_by(&:created_at).