正體中文版: 如何簡化「常常在 rails console 裡反覆輸入某些指令」的狀況

Finding your own user account in rails console is very frequent command in daily work. But key the full command is very inconvenient and waste time. You can use this technique to save time!

Solution

Edit ~/.irbrc (or ~/.pryrc if you are using pry)

class Dev
  def self.me
    User.find_by_email("your_mail@example.com")
  end
end

Then in rails console:

[1] pry(main)> user = Dev.me
  User Load (0.3ms)  SELECT `users`.* FROM `users` WHERE `users`.`email` = 'your_mail@example.com' LIMIT 1
  ...

You can put most frequent used commands in there. But watch out for name collision.

p.s. I used to put a app/models/dev.rb in every projects. But it gets inconvenient when maintaining too many projects. Also your colleagues might have a different shortcut preferences. So I decided to put them in ~/.pryrc