Rails4 - 如何调试用户对象?

时间:2015-01-24 20:53:17

标签: ruby-on-rails ruby ruby-on-rails-4 byebug

如何调试用户对象?
我正在使用byebug。我想访问name属性,但我得到了类名用户 请帮助
谢谢

[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)

我期待着名字:“sumar”

2 个答案:

答案 0 :(得分:1)

哦,我刚注意到@msguser是一种关系,而不是记录。

@msguser.first.name

应该这样做。

答案 1 :(得分:1)

我对byebug一无所知,但我相信它与它无关。如果要调试单个用户,则应该输入

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

这将为您提供一个用户而不是一组用户。我相信你之前做的是调用集合的名称,即User。另一种方法是

@msguser = User.find(params['recipients'])