How to get nice formatting in the Rails console

前端 未结 12 1476
一生所求
一生所求 2020-12-12 10:50

I want to get something like this to look nice:

>> ProductColor.all
=> [#

        
12条回答
  •  没有蜡笔的小新
    2020-12-12 11:03

    You might want to define ProductColor's inspect method to return something that you find nice. For example:

    def inspect
      "<#{id} - #{name} (#{internal_name})>"
    end
    

    After which the result of ProductColor.all will display as something like [<1 - White (White)>, ...]. Of course you should adjust the inspect method to your needs, so that it displays all the information you need in a style that you like.

    Edit: also if the issue was the lack of line breaks in the output, you might try

    require 'pp'
    pp ProductColor.all
    

    which should insert linebreaks where appropriate

提交回复
热议问题