YAML output from rail console

When I run a command like y Grau.all in the rails console, I get these strange !binary y Grau.all strings instead of the attribute name. Any idea how to fix this?

Thanks.

 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 

[UPDATE]

 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> 

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

+5
source share
3 answers

It seems that rails uses the newer psych YAML mechanism by psych , the older syck yaml mechanism does not output !binary Binary keys. If you just want to test it in the console, you can switch to the old Yamal engine as a temporary solution:

  > 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 

You will only need to do this when the keys of the name / attribute of the ActiveRecord column are encoded using Encoding::ASCII_8BIT , which, it seems to me, only happens with SQLite.


Update:

After posting this answer, SQLite3 stone has been fixed to return utf8 for column names . Make sure you are using version 1.3.6 (or higher) in sqlite3 stone. Then the default mechanism / new psych yaml (which also supports unicode output for humans) 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 
+9
source

I ran into the same problem displaying my debugging (params) on one of my pages. It disappointed me. I probably came a long way, but I used sqlite3, and knowing that I was going to use Postgres in production, I went ahead and set up the local Postgres database locally (kind of pain in the butt). Another thing that could be done was in the database.yml file, which I added the encoding: unicode .

If you have the time and patience, it is probably best to use the same database in the test that you will use in the production process.

+3
source

I'm not sure irb, but try the rails console inside your rails application root (which will load all activerecord objects correctly)

in rails 2.x

 <your rails app> ruby script/console 

in rails 3.x

 <your rails app> rails c 

amuses

-1
source

Source: https://habr.com/ru/post/910304/


All Articles