#48 Console Tricks
Jun 22, 2007 | 10 minutes |
Tools
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.
- Download:
- mp4Full Size H.264 Video (20.9 MB)
- m4vSmaller H.264 Video (11.6 MB)
- webmFull Size VP8 Video (38.8 MB)
- ogvFull Size Theora Video (28.1 MB)
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/
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
Ryan, app.assigns doesn't seem to work with symbols. app.assings['projects'] works!
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)
You can also do inline logging of SQL right in irb:
http://toolmantim.com/article/2007/2/6/system_wide_script_console_logging
If you think exiting and re-entering the console sucks, try
>> reload!
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 :)
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! :)
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.
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
Don't forget Wirble!
It's my favorite console trick.
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!
Just an FYI, I had to add
require 'rubygems'
at the top of my .irbrc file before this would work.
here is lot of console trip n trick in rails
http://madhukaudantha.blogspot.com/2009/12/ruby-on-rails-part-3-console-tips.html
FYI: http://errtheblog.com/post/43 link is dead.
How can I load the .irbrc file after the application gets initialized?
First two links had changed their address to:
http://errtheblog.com/posts/24-irb-mix-tape
http://errtheblog.com/posts/41-real-console-helpers
I have updated the links in the show notes. Thanks for letting us know! :)
This episode has been updated for Rails 5 as a blog post. Console Tricks in Rails 5