Read my latest article: Announcing RailsDeveloper (posted Wed, 01 Sep 2010 17:01:00 GMT)

Are you a console master?

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

41 comments Latest by gucci outlet Thu, 26 Aug 2010 13:01:28 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

  15. Avatar
    laptop battery factory Thu, 13 May 2010 07:02:26 GMT

    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

  16. Avatar
    jewellery earrings Fri, 28 May 2010 04:01:41 GMT

    cicelyjuicy ring tyson alvinreplica dior bags robertson hamptondolce and gabbana jewellery beach

  17. Avatar
    jewellery earrings Fri, 28 May 2010 04:02:11 GMT
  18. Avatar
    Christian Louboutin Pumps Wed, 16 Jun 2010 03:25:24 GMT

    Christian Louboutin pumps strange that the change in the name of the woman in the verb. It has allowed women to show their best form.

  19. Avatar
    Chanel handbags Sun, 20 Jun 2010 01:56:30 GMT

    Chanel bags are bogus to top standards. If you are analytic for in a artisan Chanel bag , it can admonition you atom the complete from the fake. China is a cogent artisan of afflicted and Chanel Wallet .

  20. Avatar
    air jordan 11 Wed, 30 Jun 2010 08:30:54 GMT

    Demonstrate a unique new conceptjordan shoesAwq11

  21. Avatar
    nike dunk Mon, 05 Jul 2010 17:09:45 GMT

    how to get more nike dunks

  22. Avatar
    nike dunk Mon, 05 Jul 2010 17:10:22 GMT

    how to get more nike dunks

  23. Avatar
    asics running shoes Wed, 21 Jul 2010 13:43:50 GMT
  24. Avatar
    http://www.guccishopping.com Thu, 22 Jul 2010 04:19:10 GMT
  25. Avatar
    logo design Thu, 29 Jul 2010 08:18:52 GMT

    Thanks alot mate, relevant post about how to integrate console with objects in the web session:

  26. Avatar
    EZ flash Wed, 04 Aug 2010 09:05:47 GMT

    Nintendo recently carry out its first brand promotion activity since 10 years in order to re-establish its brand image among the youth market. 53.7% of People Complain that they Can not Afford Genuine NDSL For the main transmission carrier in magazines, “who are you” as the theme, nintendo launched large-scale ads. Through a variety of video game characters, advertising campaign clearly convey a purport of the game. That is, Nintendo can make the game players to try a variety of imaginative role, and experience emotions and personality of the colorful characters.Wanna Modify Your Glamour for February? Try Nintendo DS Lite DSi Card DS Lite Console R4 Card AK2i DSTT Card M3 Adapter EZ flash vi dsi iphone 3g case

  27. Avatar
    wholesale nfl jerseys Thu, 05 Aug 2010 05:40:50 GMT

    Thanks you so much. This post is very help full for me a lot. Thanks for sharing nice post. Awesome post.

  28. Avatar
    Vibram Five Fingers Fri, 06 Aug 2010 05:40:57 GMT

    Well said. I never thought I would agree with this opinion, but I’m starting to view things from a different view. I have to research more on this as it seems very interesting. One thing I don’t understand though is how everything is related together. Christian Louboutin Christian Louboutin Shoes Christian Louboutin Boots Christian Louboutin D’orsay Vibram Five Fingers Sale Buy Vibram Five Fingers Vibram Five Fingers Bikila Vibram Five Fingers Speed

  29. Avatar
    messenger bag factory Mon, 09 Aug 2010 03:30:46 GMT

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

  30. Avatar
    asasas Tue, 10 Aug 2010 08:44:26 GMT

    All the Gucci shoes top designersGucci handbagscarrying a genuine LV bagGucci outlet store
    carrying a genuine LV bagcarrying a genuine LV baglouis vuitton top designersGucci outlet store

  31. Avatar
    Herve leger Thu, 12 Aug 2010 08:08:43 GMT

    We are a professional exporter and wholesaler of brand fashion products,herve leger Herve leger clothing on sale Herve Leger Perfume on sale Herve Leger Grey Bandage Dress Herve Leger Bandage One Shoulder Gray Dress,All products have good quality,fast and safe delivery without shipping fee. Our primary goal is to meet our clients’ requirement and establish mutually pleasant business relationships with you.If you are interested, please do not hesitate to contact us.

  32. Avatar
    Coach Handbags Fri, 13 Aug 2010 00:40:31 GMT

    We are a professional exporter and wholesaler of brand fashion products,Coach Peyton Leather Large Carryall 14510 Black Coach Peyton Leather Large Carryall 14510 Red Coach Peyton Leather Large Carryall 14510 Yellow Coach Peyton Signature Sateen Carryall 14505 Apricot-Brown Coach Peyton Signature Sateen Carryall 14507 Apricot-Brown,All products have good quality,fast and safe delivery without shipping fee. Our primary goal is to meet our clients’ requirement and establish mutually pleasant business relationships with you.If you are interested, please do not hesitate to contact us.

  33. Avatar
    Gucci Thu, 19 Aug 2010 01:21:45 GMT

    You can from ourlouis vuitton Louis Vuitton Shoes Louis Vuitton ShoesED hardy Jeanslv bags we offer here

  34. Avatar
    ghd hair straighteners Thu, 19 Aug 2010 07:52:01 GMT

    Hey, I read a lot of blogs on a daily basis and for the most part people lack substance but Vibram Five Fingers Sprint I just wanted to make a quick comment to say GREAT blog!….. I’ll be checking in on a regularly now…. Keep up the good work! CHI Bule Camo Collection

  35. Avatar
    dfgdf Mon, 23 Aug 2010 07:40:06 GMT

    wholesale nike shoesShop a great selection of authentic Nike shoes&Nike Air Max with reasonable price for the entire families at nike-shoes-max.com.nike shoes 100% quality guaranteed and smooth customer service.UGG Women’s Classic Cardy Boots 5819 are available with colorful knit uppers (composed of a wool blend) and a sheepskin sock liner for extra comfort.ugg boots It is detailed with three oversized wood buttons, allowing it to be styled buttoned up, australia uggslouched down, slightly unbuttoned, or completely cuffed down. They have a light and flexible EVA outsole along with a suede heel guard provides durable wear all season long. That is why it is one of several styles that have been all time favorites with women.

  36. Avatar
    http://www.bestretroshoes.com Tue, 24 Aug 2010 07:47:12 GMT
    Air Jordan Fusion 6,Air Jordan Fusion 6
    Jordan 7,Jordan 7
    Jordan 8,Jordan 8
    jordan ajf 6 shoes,jordan ajf 6 shoes
    jordan ajf 8 shoes,jordan ajf 8 shoes
    jordan ajf 9 shoes,jordan ajf 9 shoes
    Jordan 9,Jordan 9
    air jordan force 4,air jordan force 4
    air jordan force 9,air jordan force 9
    air jordan force 20,air jordan force 20
    Jordan 10,Jordan 10
    Jordan 11,Jordan 11
  37. Avatar
    cluburban Wed, 25 Aug 2010 13:09:13 GMT

    We are one of biggest supplier in China. Wholesale sports shoes , handbags, apparels, etc.We appriciate to establish long-term business with you, welcome to our website: http://www.cluburbanclothing.com

  38. Avatar
    cluburban Wed, 25 Aug 2010 13:09:14 GMT

    We are one of biggest supplier in China. Wholesale sports shoes , handbags, apparels, etc.We appriciate to establish long-term business with you, welcome to our website: http://www.cluburbanclothing.com

  39. Avatar
    cluburbanclothing Wed, 25 Aug 2010 13:11:12 GMT

    We are one of biggest supplier in China. Wholesale sports shoes , handbags, apparels, etc.We appriciate to establish long-term business with you, welcome to our website: http://www.cluburbanclothing.com

  40. Avatar
    shakira Wed, 25 Aug 2010 13:25:31 GMT

    fivefingers connects you to the earth and your surroundings in a way that is simply not possible in conventional shoes. vibram five fingers flow puts you in touch with the earth beneath your feet and liberates you to move in a more natural, healthy way. Vibram FiveFingers Flow Mens stimulate the muscles in your feet and lower legs to build strength and improve range of motion.

  41. Avatar
    gucci outlet Thu, 26 Aug 2010 13:01:28 GMT

    his item ships same day if ordered by 4pm Central Time . Show your team spirit with this Dallas Cowboys Tankini. Features team logo on bottom leftside of top and on back of bottom. Frills on bottom of top Made of nylon/spandex blend for great fit Officially licensed by the NFL Please Note: Womens swimwear must have sanitary liner attached for exchange or return eligibility.

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

Comments