13
13

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コンソールにPryを使う

Posted at

知らなかった。PryがRailsコンソールに使えるだなんて。

$ cd rails_some_app
$ pry -r ./config/environment

しかもいろいろ便利機能満載でびっくりしました。

モデルをサクサク取り回しできる

例えば、Userモデルにcd(えっ?)すると

pry(main)> cd User
pry(user):1> self
=> User(id: integer, name: string, age: integer, created_at: datetime, updated_at: datetime)

いちいちモデル名を頭につけなくてもよかったりする。

pry(user):1> all

pry(user):1> first

pry(user):1> find 10

ブレークポイントの設定ができる

Gemfileにpryを設定してbundle installして、

gem 'pry', :group => :development

binding.pry でブレークポイントを設定。

users_controller.rb
  def index
    @users = User.all
    binding.pry 

でrails server走らせてブラウザでアクセスすると?

[2012-06-24 00:04:23] INFO  WEBrick 1.3.1
[2012-06-24 00:04:23] INFO  ruby 1.9.3 (2012-04-20) [x86_64-darwin11.4.0]
[2012-06-24 00:04:23] INFO  WEBrick::HTTPServer#start: pid=12823 port=3000

From: /Users/hoge/testAp030303/app/controllers/users_controller.rb @ line 4 UsersController#index:

     4:   def index
     5:     @users = User.all
 =>  6:     binding.pry
     7: 
     8:     respond_to do |format|
     9:       format.html # index.html.erb
    10:       format.json { render json: @users }
    11:     end
    12:   end

[1] pry(#<UsersController>)> 

プレイクポイントでPryが立ち上がってくれる。
先に行かせたいときは

[1] pry(#<UsersController>)> exit-all

まだいろいろある。スゴイ(;´Д`)

こちらのサイトより抜粋いたしました-->Pry With Rails

13
13
3

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
13
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?