0

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

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

4

2 回答 2

1

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

@msguser.first.name

应该做的伎俩。

于 2015-01-24T20:57:55.053 回答
1

我对byebug一无所知,但我相信它与它无关。如果你想调试单个用户,你应该把

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

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

@msguser = User.find(params['recipients'])
于 2015-01-24T20:59:32.130 回答