Read my latest article: Planet Argon Blog (posted Wed, 17 Feb 2010 15:11:00 GMT)

Are you a console master?

Posted by Robby Russell Fri, 19 Aug 2005 03:06:00 GMT

14 comments Latest by Daniel Rosenstark Tue, 30 Jun 2009 13:33:45 GMT

I have a few questions.

1.) Do you know what ./script/console does?

2.) If not, why not?

3.) If so, do you have any fun tips and tricks to share with the masses?

It occured to me earlier that many people, who might have came from the PHP camp, may have never really tested their object-oriented code from some sort of interactive program. (irb) If you are coming from the Python, Java, etc worlds, interactive testing isn’t anything new. Rails is nice enough to bundle a console script right within it!

I meet people online who have never even tried to run it. There are not many tutorials on the wiki that show console… and in my opinion, its one of the coolest things about Ruby and Rails. (but, I come from the php world…)

So, if you aren’t using it… why not? got a moment? try this from the root path of your Rails application.

./script/console
It start up okay? If so, what is the name of one of your models? Let’s say that I have a model structure like:
class Customer < ActiveRecord::Base
  has_many :orders, :dependent => true
end

class Order < ActiveRecord::Base
  belongs_to :customer
end
From console, you can access your models and do all sorts of fun things.
>> y = Customer.find(16)
=> #<Customer:0x2743ea4 @attributes={"name"=>"Robby", "id"=>"16"}>
>> y.orders
=> [#<Order:0x27416b8 @attributes={"id"=>"18", "amount"=>"12.00", "customer_id"=>"16"}>, #<Order:0x274167c @attributes={"id"=>"19", "amount"=>"12.50", "customer_id"=>"16"}>] 

Pretty neat, huh?

>> o = Order.find(18)
=> #<Order:0x273da68 @attributes={"id"=>"18", "amount"=>"12.00", "customer_id"=>"16"}>
>> o.customer.name
=> "Robby" 

If you are remotely a console wizard, please share some tips and tricks for those who are not sure what to do with it. I personally find myself in console all the time that I am working with Rails, testing stuff out with my models, before I move any of the code to my application.

It sure beats, hitting refresh in your browser all day. :-)

Subscribe to my RSS feed Enjoying the content? Be sure to subscribe to my RSS feed.
Comments

Leave a response

  1. Avatar
    Lance Wed, 02 Aug 2006 11:58:30 GMT

    Hi Robby,

    Like yourself, I am happy to use the console a lot. I was wondering if there is anyway to get your model objects to list only the methods that you have added and also their relationships with other model objects. For instance to get your author object to list its associated books etc.

    Thanks a lot, and keep up the good work. Look forward to seeing your patch to help rails use postgres int[] arrays make it into core.

  2. Avatar
    Chris Fri, 15 Sep 2006 13:50:06 GMT

    I would like to try out the console but I cannot get it to load. The console.rb file contains the following.

    #!/usr/bin/env ruby require File.dirname(FILE) + ’/../config/boot’ require ‘commands/console’

    When I try to load it I get this message.

    c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require’: no such file to load—./script/../config/../config/environment (LoadError) from c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require’ from c:/ruby/lib/ruby/1.8/irb/init.rb:252:in `load_modules’ from c:/ruby/lib/ruby/1.8/irb/init.rb:250:in `load_modules’ from c:/ruby/lib/ruby/1.8/irb/init.rb:21:in `setup’ from c:/ruby/lib/ruby/1.8/irb.rb:54:in `start’ from c:/ruby/bin/irb.bat:20

  3. Avatar
    Marc Thu, 09 Nov 2006 02:13:36 GMT

    For Chris in comment 2:

    If you are using Windows, from your root directory: ruby script/console

  4. Avatar
    Konstantin Gredeskoul Thu, 07 Dec 2006 02:05:49 GMT

    Here is a relevant post about how to integrate console with objects in the web session:

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

  5. Avatar
    Konstantin Gredeskoul Thu, 07 Dec 2006 02:06:42 GMT

    Here is a relevant post about how to integrate console with objects in the web session:

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

  6. Avatar
    Akhil Bansal Mon, 25 Dec 2006 10:22:44 GMT

    If you know the session_id (you can get session_id from the cookie) then you can get the data from session as described here.

  7. Avatar
    Akhil Bansal Mon, 25 Dec 2006 10:23:04 GMT

    If you know the session_id (you can get session_id from the cookie) then you can get the data from session as described here.

  8. Avatar
    Stephen Thu, 01 Nov 2007 15:24:32 GMT

    Amy Hoy has a good article which also serves as a great cheat sheet.

    http://www.slash7.com/articles/2006/12/21/secrets-of-the-rails-console-ninjas

  9. Avatar
    uma mahesh Thu, 28 Feb 2008 07:09:43 GMT

    Thanks alot for this post

  10. Avatar
    retronerd Wed, 16 Jul 2008 18:09:52 GMT

    Hey Lance you could try,

    reflect_on_all_associations(:has_many/:belongs_to)

  11. Avatar
    ajey Wed, 21 Jan 2009 06:17:31 GMT

    How do I run a action in controller from ruby console.

  12. Avatar
    Christopher Tue, 26 May 2009 03:45:05 GMT

    something I do all the time to get a useful list of methods:

    Foo.methods.sort.select{|a| not Object.respond_to? a}

  13. Avatar
    Daniel Rosenstark Tue, 30 Jun 2009 12:54:44 GMT

    @ajey, check out this post

    http://snippets.dzone.com/posts/show/600

    about getting into the controller. I might post something soon based on the comments in that same post.

  14. Avatar
    Daniel Rosenstark Tue, 30 Jun 2009 13:33:45 GMT

    Hi Robby, thanks for the article. Here is my short (and derivative :) article on how to get at the controller with the console…

    http://compileyouidontevenknowyou.blogspot.com/2009/06/using-rails-console-for-controller.html

Share your thoughts... (really...I want to hear them)

Comments