轨控制台的YAML输出 [英] YAML output from rails console

查看:131
本文介绍了轨控制台的YAML输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当在rails console中执行 y Grau.all 命令时,我会得到这些奇怪的!binary 的属性名称。任何想法如何解决这个问题?

When executing a command like y Grau.all in rails console I am getting these strange !binary strings instead of the attribute's name. Any idea how to fix this?

谢谢。

irb(main):003:0> y Grau.all
  ←[1m←[36mGrau Load (0.0ms)←[0m  ←[1mSELECT "graus".* FROM "gr
  ←[1m←[35mEXPLAIN (0.0ms)←[0m  EXPLAIN QUERY PLAN SELECT "grau

EXPLAIN for: SELECT "graus".* FROM "graus"
0|0|0|SCAN TABLE graus (~1000000 rows)

---
- !ruby/object:Grau
  attributes:
    !binary "aWQ=": 27
    !binary "bm9tZQ==": 1 Grau
    !binary "Y3JlYXRlZF9hdA==": 2012-04-06 21:24:34.553163000 Z
    !binary "dXBkYXRlZF9hdA==": 2012-04-06 21:24:34.553163000 Z
- !ruby/object:Grau
  attributes:
    !binary "aWQ=": 28
    !binary "bm9tZQ==": 2 Grau
    !binary "Y3JlYXRlZF9hdA==": 2012-04-06 21:24:34.599963000 Z
    !binary "dXBkYXRlZF9hdA==": 2012-04-06 21:24:34.599963000 Z

[UPDATES]

[UPDATES]

irb(main):001:0> Grau.find(1)
  ←[1m←[36mGrau Load (43.8ms)←[0m  ←[1mSELECT "graus".* FROM "graus" WHERE "grau
s"."id" = ? LIMIT 1←[0m  [["id", 1]]
=> #<Grau id: 1, nome: "1º Grau", created_at: "2012-04-11 15:51:32", updated_at:
 "2012-04-11 15:51:32">
irb(main):002:0>

我在Windows 7 64位上使用Rails 3.2.3,Ruby 1.9.3。 p>

I am using Rails 3.2.3, Ruby 1.9.3 on a Windows 7 64 bit.

推荐答案

这似乎是因为rails默认使用较新的 psych YAML引擎,旧的 syck yaml引擎不会输出!binary 键。如果你只是想在控制台中测试,你可以切换回旧的yaml引擎作为临时解决方法:

It appears to be because rails defaults to using the newer psych YAML engine, the older syck yaml engine doesn't output the !binary keys. If you're just wanting it for testing in the console you can switch back to the older yaml engine as a temp workaround:

 > y User.first
  User Load (0.0ms)  SELECT "users".* FROM "users" LIMIT 1
--- !ruby/object:User
attributes:
  !binary "aWQ=": 1
  !binary "bmFtZQ==": Example User
  !binary "ZW1haWw=": user@example.com

 > YAML::ENGINE.yamler= 'syck'
=> "syck"

 > y User.first
  User Load (1.0ms)  SELECT "users".* FROM "users" LIMIT 1
--- !ruby/object:User
attributes:
  id: 1
  name: Example User
  email: user@example.com

你只需要这样做,当你的ActiveRecord列名/属性键使用 Encoding :: ASCII_8BIT 编码,我认为只发生在SQLite。

You would only need to do this when your ActiveRecord column names/attributes keys are encoded using Encoding::ASCII_8BIT which I think only happens with SQLite.

自从发布此答案后,SQLite3 gem已修复为返回utf8的列名称。确保您使用的是1.3.6(或更高版本)的sqlite3 gem。然后,默认/新的psych yaml引擎(也支持人类可读的unicode输出)将正常工作:

Since posting this answer the SQLite3 gem has been fixed to return utf8 for column names. Make sure you're using version 1.3.6 (or higher) of the sqlite3 gem. Then the default/newer psych yaml engine (which also supports human-readable unicode output) will work without problems:

 > y User.first
  User Load (0.0ms)  SELECT "users".* FROM "users" LIMIT 1
  EXPLAIN (0.0ms)  EXPLAIN QUERY PLAN SELECT "users".* FROM "users" LIMIT 1
EXPLAIN for: SELECT  "users".* FROM "users"  LIMIT 1
0|0|0|SCAN TABLE users (~1000000 rows)

--- !ruby/object:User
attributes:
  id: 1
  name: irmão
  email: user@example.com

这篇关于轨控制台的YAML输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆