Rails select语句返回hash。怎么了?

时间:2015-01-11 17:52:26

标签: ruby-on-rails

我写道:

Profcover.select("cover_name").where(username: username).last

并在日志中正确返回:

SELECT  "profcovers"."cover_name" FROM "profcovers"  WHERE "profcovers"."username" = 'alizade'  ORDER BY "profcovers"."id" DESC LIMIT 1

当我将其粘贴到SQL时,它会返回正确的答案。但是当我将其粘贴到代码中时,它打印的不是/assets/covers/3.jpg而是#<Profcover:0x007f9e6d2ca430>。有什么问题?

1 个答案:

答案 0 :(得分:1)

使用

Profcover.select("cover_name").where(username: username).last.cover_name

Profcover.select("cover_name").where(username: username).last返回Profcover个对象,其中<Profcover id: nil, cover_name: "....">为内容。