Ask A Question

Notifications

You’re not receiving notifications from this thread.

Added last_read_at to chatroom_users_controller: No method error

Rob Sturcke asked in General

Added the following to my chatroom_users_controller but keep running into a NoMethod Error.

def create
    @chatroom_user = @chatroom.chatroom_users.where(user_id: current_user.id).first_or_create
    @chatroom_user.update(last_read_at: Time.zone.now)
    redirect_to @chatroom
  end

Just a bit confused as to what else I may be missing since I keep getting the chatrooms/show.html.erb error

<% if !unread_messages && @chatroom_user.last_read_at < message.created_at %>
Reply

If it's a no method error, make sure you ran the migration to add that column to the ChatroomUser model. The no method error usually comes up when a column doesn't exist that you try to access.

Reply

Oh I thought that was completed with rails g migration AddLastReadAtToChatroomUsers last_read_at:datetime

Reply

Yep, that looks right. Your Rails app doesn't seem to know that exists though which means there's some discrepancy. Maybe try restarting your Rails server in case it didn't pickup the migration or there might be a typo somewhere I'm not noticing.

Or maybe you've got a different error than the one I'm assuming you've got. Can you paste the full error?

Reply

That's strange, I've restarted my rails server previously and checked the migration, here's the migration contents:

class AddLastReadAtToChatroomUsers < ActiveRecord::Migration[5.0]
  def change
    add_column :chatroom_users, :last_read_at, :datetime
  end
end

Full Error:

NoMethodError in Chatrooms#show

undefined method `last_read_at' for nil:NilClass

<% if !unread_messages && @chatroom_user.last_read_at < message.created_at %>
Reply

Ah, so the error is saying your @chatroom_user record is nil (didn't get saved for some reason) and therefore your last_read_at didn't work. Slight different than the error I was originally thinking of. Check your @chatroom_user gets set properly in the controller and why that might not be set.

Reply

I believe I introduced a bug related to this at one point which you might be running into. Check out the code on Github in the notes on the episode and you might spot the fix I introduced. Can't remember exactly what it was.

Reply

Went back in terminal to double check, looks like things went through:

$ rails g migration AddLastReadAtToChatroomUsers last_read_at:datetime
      invoke  active_record
      create    db/migrate/20161016055506_add_last_read_at_to_chatroom_users.rb
$ rake db:migrate
== 20161016055506 AddLastReadAtToChatroomUsers: migrating =====================
-- add_column(:chatroom_users, :last_read_at, :datetime)
   -> 0.0058s
== 20161016055506 AddLastReadAtToChatroomUsers: migrated (0.0059s) ============

$ rails g channel LastRead
      create  app/channels/last_read_channel.rb
   identical  app/assets/javascripts/cable.js
      create  app/assets/javascripts/channels/last_read.coffee
Reply

I'll check out the direct messages commit, was looking through the fixes submitted by Shakycode but I believe I added this to my method already:
https://github.com/gorails-screencasts/chatrooms/commit/595c3cbc87a1b77576af178789c3dc30ef53313c

Reply

Ah yep. Hmm, so I guess then the thing to do is debug why @chatroom_user is set to nil instead of a record. Does your chatrooms#show set the @chatroom_user record? https://github.com/gorails-screencasts/chatrooms/blob/master/app/controllers/chatrooms_controller.rb#L14

Reply

If you are pulling from the gorails chatrooms repo, I did a PR against this repo which fixes the nil issue. You can either clone the repo and use it as reference for your own iteration or clone it and just hack on the existing repo (suggest forking into your own github account thought :)

Reply

I actually got it to work later with the direct messages video which I forgot was part of the series after switching to the public_channel I believe.

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 87,563+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.