0

got a super quick question. I'm still new to rails and tried following these two questions but they didn't work for me:Why does Array.to_s return brackets? and ruby 1.9 how to convert array to string without brackets.

I'm trying to show the last message and the date in which it was sent out in my chatroom application. I am able to get the results using this code, but it has brackets around it and I would like to have those brackets removed. Any help here would be amazing, I've attached a screenshot as well. Thank you so much!

Show.html.erb

For the Date: <%= chatroom.messages.last(1).pluck(:created_at) %> For the Last Message in Chatroom: <%= chatroom.messages.last(1).pluck(:body) %>

DirectMessages Controller

class DirectMessagesController < ApplicationController
before_action :authenticate_user!

def show
    users = [current_user, User.find(params[:id])]
    @messageduser = User.find(params[:id])
    @chatroom = Chatroom.direct_message_for_users(users)        
    @chatroomall = current_user.chatrooms
    @messages = @chatroom.messages.order(created_at: :desc).limit(100).reverse
    @messagelast = @chatroom.messages.last(1)
    last_message = @chatroom.messages.last
    render "chatrooms/show"
end
private

def chatroomuserlocator
    @chatroomlocator = Chatroom.find(params[:chatroom_id])
end
end

enter image description here

Omar
  • 383
  • 3
  • 17

3 Answers3

1

If you're not too worried about memory usage, you can fetch the whole object and only access the fields you want.

<%= chatroom.messages.last.created_at %>
<%= chatroom.messages.last.body %>
urielSilva
  • 400
  • 1
  • 11
  • Thanks @urielSilva for responding, I tried doing that got this error message "undefined method `created_at' for nil:NilClass" I've also put my controller in case that helps. – Omar Aug 17 '17 at 01:12
  • You can use the `last_message` variable used in the controller. Just make sure you add the `@` at the beginning of the variable's name, both in the view and controller – urielSilva Aug 17 '17 at 01:24
1

Try this:

<%= chatroom.messages.last.created_at %>

And this:

<%= chatroom.messages.last.body %>

Keep in mind that pluck returns an array, so that would explain your brackets.

I don't think you need pluck here since you are just accessing an attribute on a single item.

Dan Kreiger
  • 5,358
  • 2
  • 23
  • 27
  • Thanks @Dan for responding and for the context, I tried doing that got this error message "undefined method `created_at' for nil:NilClass" – Omar Aug 17 '17 at 01:08
  • The last one using ".try" worked!! Thanks a billion Dan!! – Omar Aug 17 '17 at 01:18
  • No problem. I'm glad it helped. I'm just updating my comment here because I had a typo in it. If you want to remove `nil` elements from your messages array, you can use the [compact](https://apidock.com/ruby/v1_9_3_392/Array/compact) method like this: `<%= chatroom.messages.compact.last.created_at %>`. If that returns an error, you probably don't have any messages in your chatroom instance. If you just want to avoid returning an error, you can use the [try](https://apidock.com/rails/v3.2.1/Object/try) method like this: `<%= chatroom.messages.last.try(:created_at) %>` – Dan Kreiger Aug 17 '17 at 01:30
1

You can assign the lookup to a value, so it doesn't run twice:

last_message = chatroom.messages.last

Then you can access the attributes efficiently:

last_message.created_at
last_message.body

If you are interested in limiting the attributes or last_message, use select:

last_message = chatroom.messages.select(:created_at, :body).last

Putting it all together:

<% last_message = chatroom.messages.select(:created_at, :body).last %>
<%= last_message.created_at %>
<%= last_message.body %>
Brad Werth
  • 17,411
  • 10
  • 63
  • 88
  • Thanks @Brad I tried doing what you suggest. Added `last_message = @chatroom.messages.last` in my DirectMessages Controller. And then in my show views file I added the line `<%= last_message.created_at %>` I got an error message "undefined local variable or method `last_message' for #<#:0x007fe33d6aae28>" – Omar Aug 17 '17 at 01:06
  • 1
    @Omar you wouldn't put that in the controller, you would keep it in the view, in the erb as described. If you wanted it in the controller, you would need to use `@last_message`... – Brad Werth Aug 17 '17 at 05:20