76
66

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Railsでよく使うDebug

Posted at

Rails で object の中身をログに出力したいときがある。
覚えたと思っても、使いたいときには大抵忘れている。

ということで、Railsでよく使うものの自分用メモ。

object の中身をログに出力

オブジェクトをそのまま渡すとこんな出力になってしまう。

logger.debug @users
#<User::ActiveRecord_Relation:0x007fd25d4aca40>

これでは困る。
そこで、inspect でわかりやすく出力。

logger.debug @users.inspect
#<ActiveRecord::Relation [#<User id: 1, name: "hogehoge", ... 

見やすさ重視なら、yaml フォーマットで出力。

logger.debug @seekers.to_yaml
---
- !ruby/object:User
  attributes:
    id: 1
    name: hogehoge

logger.debug が使えないところでは

Rails.logger.debug "hogehoge"
76
66
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
76
66

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?