#48
Jun 22, 2007

Console Tricks

The Rails console is one of my favorite tools. This episode is packed with tips and tricks on how to get the most out of the console.
Tags: tools
Download (20.8 MB, 10:51)
alternative download for iPod & Apple TV (13.6 MB, 10:51)

Resources

RSS Feed for Episode Comments 15 comments

1. chineseGuy Jun 22, 2007 at 00:38

very cool!~


2. Jermaine Jun 22, 2007 at 01:21

Ryan, very useful !

Another cool and powerful thing is that you can load (web)session objects straight into your console.

The how-to on this is written on:

http://tektastic.wordpress.com/2006/12/06/ruby-on-rails-how-to-load-session-objects-into-console/


3. Jan Jun 22, 2007 at 02:43

You say your models don't always show up in autocompletion. This is just a case of lazy loading, console doesn't know your classes until you've loaded them (you did Project.new in the beginning, which loaded it). Once console's loaded/required your model, it'll auto-complete


4. weskycn Jun 22, 2007 at 04:34

很好,实在是太牛了。。


5. Andreas Jun 22, 2007 at 05:09

Ryan, app.assigns doesn't seem to work with symbols. app.assings['projects'] works!


6. Geoff Buesing Jun 22, 2007 at 10:45

Great stuff -- I had no idea that 'y' method existed, very handy!

Re assigns -- you can use a symbol if you call the assigns method: app.assigns(:projects)


7. Carl Porth Jun 22, 2007 at 12:30

You can also do inline logging of SQL right in irb:

http://toolmantim.com/article/2007/2/6/system_wide_script_console_logging


8. Curtis Miller Jun 22, 2007 at 16:45

If you think exiting and re-entering the console sucks, try

>> reload!


9. Nicolás Sanguinetti Jun 22, 2007 at 17:22

Nice tips! :D

A great source of knowledge (and magic tricks!) for my irbrc has been dotfiles.org

There are a bunch of nice .irbrc posted there, as there are some sweet .bashrc and .vimrc files, which might come in handy to some of you :)


10. Javier Segura Jun 27, 2007 at 10:31

I have a link to this screencast in http://thinkingeek.com/index.php?/archives/17-Customizing-IRB.html because I think it's a great one! :)


11. Gavin Stark Jun 27, 2007 at 16:16

script/console isn't being "flaky" -- I believe the reason why your "name" method on your project didn't autocomplete until you accessed it because the method is created in the class by ActiveRecord::Base when it notices it doesn't exist (through a call to method_missing) See ActiveRecord::Base define_read_methods.


12. Mike Stramba Jul 06, 2007 at 18:18

Often I just want to assign something to a variable, but I don't want to see tons of output, e.g. actors = Actor.find_all.

I discovered that I can eliminate the (large) output by appending a "dummy" command that returns nil or something short like 1.

E.g.
actors = Actor.find_all ; 1
or
actors = Actor.find_all ; actors.size

Mike
Mike


13. Mark Cotner Nov 14, 2007 at 10:56

Don't forget Wirble!

It's my favorite console trick.


14. noodl Dec 12, 2007 at 12:26

Hey,

Just wanted to point out that script/console production --sandbox can be dangerous. Any models objects you load up that way will lock the database for those rows (assuming your db's transactions use a certain form of lock), effectively stopping your site from running. Use with care!


15. Terry Schmidt May 14, 2008 at 10:24

Just an FYI, I had to add

require 'rubygems'

at the top of my .irbrc file before this would work.

Add your comment:

(SKIP THIS ONE)

(required)

(not shown)


(use pastie or gist for code)

sponsored by:
if you want to help:
required:
Get Quicktime Player