Rails logging methods are wonderful for development.
Indeed, they often suck when you want to inspect objects or cascaded objects. So here is a codesnipped that helps you inspect any kind of any object in rails, deeply and detailed.
as a new lib-file or, just as i do, in application_controller.rb, add the follogwing code:
1 2 3 4 5 6 7 8 9 10 11 | require 'pp' require 'stringio' module Kernel def pp_s s = StringIO.new PP.pp(self, s) s.rewind s.read end end |
now anywhere in the code, lets say you want to inspect a model, or a hash, or a parameter of any unknown type:
1 | logger.warn var_of_any_kind.pp_s #or use .info |
here is an example from irb:
1 2 3 4 5 6 | >> User.last.pp_s => "#<User id: 918984, email: \"foo@baz.net\", crypted_password: \"e8878b6c722ec64b921be4c98765431263ebe813293a6c995fe88...\", password_salt: \"jfDuRTvSomfg 4Q6sCjhNm\", persistence_token: \"464af1170bf8b1302ea3f108e03ed5ff3a0568417b084af27ef...\", login_count: 2, created_at: \"2010-02-01 11:24:16\", updated_at: \"2010-02-01 11:40:40\", newsletter: \"0\">\n" >> User.pp_s => "User(id: integer, email: string, crypted_password: string, password_salt: string, persistence_token: string, login_count: integer, created_at: datetime, updated_at: datetime, newsletter: integer)\n" >> |
Write a Comment