Search code examples
ruby-on-railsrubyruby-on-rails-4byebug

Rails4 - How to debug a user object?


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"


Solution

  • 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'])