How do I print out the contents of an object in Rails for easy debugging?

我想我正在尝试获得相当于 print_r()(打印人类可读)的 PHP; 目前的原始输出是:

ActiveRecord::Relation:0x10355d1c0

我该怎么办?

124544 次浏览

在模型中定义 to _ s 方法

class Person < ActiveRecord::Base
def to_s
"Name:#{self.name} Age:#{self.age} Weight: #{self.weight}"
end
end

然后,当您使用 # put 打印它时,它将显示包含这些变量的字符串。

我通常首先尝试 .inspect,如果它不能给我我想要的,我将切换到 .to_yaml

class User
attr_accessor :name, :age
end


user = User.new
user.name = "John Smith"
user.age = 30


puts user.inspect
#=> #<User:0x423270c @name="John Smith", @age=30>
puts user.to_yaml
#=> --- !ruby/object:User
#=> age: 30
#=> name: John Smith

希望能帮上忙。

你需要使用 debug(@var),它就像“ print _ r”一样。

我用的是 awesome_print gem

所以你只需要输入:

ap @var

在 Rails 中,可以使用调试的 Helper ActionView: : Helpers: : DebugHelper在 View 中打印结果

# app/view/controllers/post_controller.rb
def index
@posts = Post.all
end


#app/view/posts/index.html.erb
<%= debug(@posts) %>


#start your server
rails s

结果(以浏览器显示)

- !ruby/object:Post
raw_attributes:
id: 2
title: My Second Post
body: Welcome!  This is another example post
published_at: '2015-10-19 23:00:43.469520'
created_at: '2015-10-20 00:00:43.470739'
updated_at: '2015-10-20 00:00:43.470739'
attributes: !ruby/object:ActiveRecord::AttributeSet
attributes: !ruby/object:ActiveRecord::LazyAttributeHash
types: &5
id: &2 !ruby/object:ActiveRecord::Type::Integer
precision:
scale:
limit:
range: !ruby/range
begin: -2147483648
end: 2147483648
excl: true
title: &3 !ruby/object:ActiveRecord::Type::String
precision:
scale:
limit:
body: &4 !ruby/object:ActiveRecord::Type::Text
precision:
scale:
limit:
published_at: !ruby/object:ActiveRecord::AttributeMethods::TimeZoneConversion::TimeZoneConverter
subtype: &1 !ruby/object:ActiveRecord::Type::DateTime
precision:
scale:
limit:
created_at: !ruby/object:ActiveRecord::AttributeMethods::TimeZoneConversion::TimeZoneConverter
subtype: *1
updated_at: !ruby/object:ActiveRecord::AttributeMethods::TimeZoneConversion::TimeZoneConverter
subtype: *1

inspect很棒,但有时还不够好。

To have full control over what's printed you could redefine to_s or inspect methods. Or create your own one to not confuse future devs too much.

  class Something < ApplicationRecord


def to_s
attributes.map{ |k, v| { k => v.to_s } }.inject(:merge)
end


end

这将对所有属性应用一个方法(即 to_s)。

您也可以只重新定义少数几个属性:

  def to_s
attributes.merge({ my_attribute: my_attribute.to_s })
end

您还可以创建两个或 以某种方式添加关联的混合。

.inspect is what you're looking for, it's way easier IMO than .to_yaml!

user = User.new
user.name = "will"
user.email = "will@example.com"


user.inspect
#<name: "will", email: "will@example.com">

Pp 也做这项工作,不需要 gem。

@a = Accrual.first ; pp @a


#<Accrual:0x007ff521e5ba50
id: 4,
year: 2018,
Jan: #<BigDecimal:7ff521e58f08,'0.11E2',9(27)>,
Feb: #<BigDecimal:7ff521e585d0,'0.88E2',9(27)>,
Mar: #<BigDecimal:7ff521e58030,'0.0',9(27)>,
Apr: #<BigDecimal:7ff521e53698,'0.88E2',9(27)>,
May: #<BigDecimal:7ff521e52fb8,'0.8E1',9(27)>,
June: #<BigDecimal:7ff521e52900,'0.8E1',9(27)>,
July: #<BigDecimal:7ff521e51ff0,'0.8E1',9(27)>,
Aug: #<BigDecimal:7ff521e51bb8,'0.88E2',9(27)>,
Sep: #<BigDecimal:7ff521e512f8,'0.88E2',9(27)>,
Oct: #<BigDecimal:7ff521e506c8,'0.0',9(27)>,
Nov: #<BigDecimal:7ff521e43d38,'0.888E3',9(27)>,
Dec: #<BigDecimal:7ff521e43478,'0.0',9(27)>,

还可以打印对象的两个实例:

 pp( Accrual.first , Accrual.second)
`
`
`

instance.attributes打印 Rails 控制台中的整个散列。