wsm
wsm

Reputation: 143

Sort by date in descending order in Ruby on Rails

I would like to sort my list in a descending order by date which as a "New user list", in the database I have a column which is

t.datetime "created_at",                                      null: false  

This is the time when a new user registered, in the view, I have the code like this:

%table.table.table-striped.table-hover
      %thead
      %h3 New Users
      %hr
        %th Name
        %th Company
        %th Role
        %th Created date
        %th{:width => '50'}
        %th{:width => '50'}
        %th{:width => '50'}
      %tbody
      - @users.each do |user|
        -if user.role == "trial-member"
          - @created_at.sort{|a,b| b.created_at <=> a.created_at}.each do |created_at|
            %tr
            %td
              = user.first_name
              = user.last_name
            %td= user.company
            %td= user.role
            %td= user.created_at
            %td= link_to 'Approve', edit_user_path(user),  {:class => 'btn btn-success btn-sm'}

but this gives an error that "undefined method `sort' for nil:NilClass", what shall I do to sort the list in table descending by created date? Thank you.

Upvotes: 13

Views: 31042

Answers (3)

Katerina German
Katerina German

Reputation: 11

I don't see where you create variable @create_at. But if you need to sort users, you can try this way:

%table.table.table-striped.table-hover
      %thead
      %h3 New Users
      %hr
        %th Name
        %th Company
        %th Role
        %th Created date
        %th{:width => '50'}
        %th{:width => '50'}
        %th{:width => '50'}
      %tbody
      - @default_create_at = Time.now
      - @users.sort_by { |u| u.created_at ? u.created_at : @default_create_at }.each do |user|
        -if user.role == "trial-member"
            %tr
            %td
              = user.first_name
              = user.last_name
            %td= user.company
            %td= user.role
            %td= user.created_at ? user.created_at : @default_create_at
            %td= link_to 'Approve', edit_user_path(user),  {:class => 'btn btn-success btn-sm'}

Upvotes: 0

Neeraj Kumar
Neeraj Kumar

Reputation: 7511

It's because @created_at is not defined anywhere. So, any instance variable which is not defined, by default returns nil. If you want to display the users in sorted order, then you need to fetch @users in order.

@users = User.order(created_at: :desc)

Upvotes: 1

Surya
Surya

Reputation: 15992

In your controller:

@users = User.order('created_at DESC')

Just add: order('created_at DESC') in your logic where you're fetching @users.

In your view, you can now get rid off of - @created_at.sort{|a,b| b.created_at <=> a.created_at}.each:

%h3 New Users
%table.table.table-striped.table-hover
  %thead
    %tr
      %th Name
      %th Company
      %th Role
      %th Created date
      %th{:width => '50'}
      %th{:width => '50'}
      %th{:width => '50'}
  %tbody
    - @users.each do |user|
      -if user.role == "trial-member"
        %tr
          %td
            = user.first_name
            = user.last_name
          %td= user.company
          %td= user.role
          %td= user.created_at
          %td= link_to 'Approve', edit_user_path(user),  {:class => 'btn btn-success btn-sm'}

Error you are seeing is because @created_at is not an enumerable object, hence it does not respond to sort.

Upvotes: 25

Related Questions