0

How do I debug a user object?
I am using byebug. I want to access the name attribute but I am getting the class name user.
Kindly help
Thanks

[8, 17] in /home/ubuntu/workspace/converse/app/controllers/messages_controller.rb
    8:   end
    9: 
   10:   def new1
   11:     @msguser = User.where(id: params['recipients'])
   12:     byebug
=> 13:     @recid = params['recipients'] 
   14:   end  
   15:   
   16:   def create
   17:     recipients = User.where(id: params['recipients'])
(byebug) 
(byebug) @msguser
  User Load (0.5ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ?  [["id", 1]]
#<ActiveRecord::Relation [#<User id: 1, name: "sumar", email: "chuch@gmail.com", created_at: "2015-01-23 12:57:41", updated_at: "2015-01-23 12:59:13", password_digest: "$2a$10$xNvGKpPwspaeTd33QrTwuuLP0TesuuTmfbrUlFO7LDs...", remember_digest: nil, admin: false, date_of_birth: "1985-01-01 00:00:00", is_female: false, activation_digest: "$2a$10$nIwWkX2X8WugscC/Yre7KeWRIzw6ecNyA5hLn/xPw9C...", activated: true, activated_at: "2015-01-23 12:58:58", reset_digest: nil, reset_sent_at: nil, avatar_file_name: nil, avatar_content_type: nil, avatar_file_size: nil, avatar_updated_at: nil, country_id: 1, cover_file_name: nil, cover_content_type: nil, cover_file_size: nil, cover_updated_at: nil, intro: "hey">]>
(byebug) @msguser.name
"User"
(byebug)

I am expecting name: "sumar"

New to Rails
  • 2,824
  • 5
  • 27
  • 43

2 Answers2

1

Ohh I just noticed that @msguser is a relation not the record.

@msguser.first.name

should do the trick.

KARASZI István
  • 30,900
  • 8
  • 101
  • 128
  • Could you please share what is a relation and how it is a relation and not a record? – New to Rails Jan 24 '15 at 21:04
  • That would work too. All depends on if he wants to have a relation and not just a single user. – Ryan K Jan 24 '15 at 21:05
  • 1
    A relation is (potentially) multiple records, kinda like an array but much more powerful. You have a relation because you used the `where` method and not `find`. Updating my answer. – Ryan K Jan 24 '15 at 21:06
1

I don't know anything about byebug but I believe it has nothing to do with it. If you want to debug a single user, you should put

@msguser = User.where(id: params['recipients']).first

That will give you one user instead of a collection of user(s). What I believe you were doing before was calling the name of the collection, which would be User. Another way of doing it is

@msguser = User.find(params['recipients'])
Ryan K
  • 3,985
  • 4
  • 39
  • 42