Marcos R. Guevara
Marcos R. Guevara

Reputation: 6388

If sentence on Rails

This code comes from the controller(class MainController < ApplicationController) On console i can view the content from puts wallMessage.messagedestiny and puts current_user.id but on the next lines don't shows anything inside the sentence where the values are the same, here come the code:

Message.each do |wallMessage|
        puts wallMessage.messagedestiny
        puts current_user.id
        if wallMessage.messagedestiny == current_user.id # <-- from here don't show anything from the following content in the sentence
          puts "*** entra en el each y devuelve el wallMessage"
          puts wallMessage
          @wallMessages.push(wallMessage.messagecontent)
          User.find_by(id: wallMessage.messagesender) do |messageSender|
            @messageSenders.push(messageSender.username)
          end
        end
      end

Upvotes: 0

Views: 31

Answers (2)

Arun Kumar Mohan
Arun Kumar Mohan

Reputation: 11915

If the if statement didn't get executed, its clear that the type of the values don't match.

Try this and you can check if for yourself.

puts wallMessage.message‌​destiny.class # displays the type of object
puts current_user.id.class

or

puts wallMessage.message‌​destiny.class == current_user.id.class

You can convert the type of one of the values to the other and compare them.

Upvotes: 1

Marcos R. Guevara
Marcos R. Guevara

Reputation: 6388

Solved!

if wallMessage.messagedestiny == String(current_user.id) # <-- Solution is convert to String cause current_user.id.class on console returned a BSON object
          puts "*** entra en el each y devuelve el wallMessage"
          puts wallMessage
          @wallMessages.push(wallMessage.messagecontent)
          User.find_by(id: wallMessage.messagesender) do |messageSender|
            @messageSenders.push(messageSender.username)
          end
        end

Upvotes: 0

Related Questions