In this question I asked how to list out users currently online in the chat. While discussing I was given an answer, and I tried to implement it, but I didn't succeed. So, I made a migration add_last_online_to_users last_online:datetime
and made a tweak in User
model:
user.rb
:
def self.online_now
where ("last_online > ?", 15.minutes.ago)
end
And then added to my Message
controller, so that each time an user sends a message, his last_online
updates to Time.now
. Finally, I added to my application_controller
the following code:
def show_online
@users = User.online_now
end
and called that method in my view:
<% @users.each do |user| %>
<%= user.show_online %>
<% end %>
that returned me a NoMethodError: undefined method 'each' for nil:NilClass