Ruby on rails rails控制台和输入端的结果不同

Ruby on rails rails控制台和输入端的结果不同,ruby-on-rails,Ruby On Rails,当我使用put(@participantt=Participant.where(id:1))时,我在控制台中得到 Participant Load (0.3ms) SELECT "participants".* FROM "participants" WHERE "participants"."id" = $1 [["id", 1]] ↳ app/controllers/interviews_controller.rb:119:in `puts' #<Participant:0

当我使用
put(@participantt=Participant.where(id:1))
时,我在控制台中得到

  Participant Load (0.3ms)  SELECT "participants".* FROM "participants" WHERE "participants"."id" = $1  [["id", 1]]
  ↳ app/controllers/interviews_controller.rb:119:in `puts'
#<Participant:0x000000000c778bf0>

为什么会这样?

在打印传递给它的表达式(将是对象)的结果之前,将
调用设置为打开。它通常打印带有对象id的类名

这里,
@participant=participant的结果。其中(id:1)
是具有id的参与者,并存储在实例变量
@participant

@participant
传递到
put
将首先调用
@participant.to\u
,然后再打印它


如果只是
@participant=participant.where(id:1)
,控制台将显示典型的REPL实用程序所显示的结果。如果你在那里做了
put@participant
,那么你将再次得到与你问题中的
put
相同的结果。

那么如何在
put
中正确打印你可以使用
p
方法或使用
put对象。检查
谢谢,我收到了。您能告诉我如何访问@participant
@participant.email
超级简单。我建议你学习Ruby的基础知识。从长远来看,StackOveflow对您没有帮助。
  Participant Load (0.7ms)  SELECT "participants".* FROM "participants" WHERE "participants"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 11]]
=> #<ActiveRecord::Relation [#<Participant id: 1, name: "Ram", email: "Ram@g.com", created_at: "2020-05-08 08:19:00", updated_at: "2020-05-08 08:19:00">]>