<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Robby on Rails: Are you a console master?</title>
    <link>http://www.robbyonrails.com/articles/2005/08/18/are-you-a-console-master</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>thoughts.sort_by{|t| t[:topic]}.collect </description>
    <item>
      <title>Are you a console master?</title>
      <description>&lt;p&gt;I have a few questions.&lt;/p&gt;


	&lt;p&gt;1.) Do you know what &lt;code&gt;./script/console&lt;/code&gt; does?&lt;/p&gt;


	&lt;p&gt;2.) If not, why not?&lt;/p&gt;


	&lt;p&gt;3.) If so, do you have any fun tips and tricks to share with the masses?&lt;/p&gt;


	&lt;p&gt;It occured to me earlier that many people, who might have came from the &lt;span class="caps"&gt;PHP&lt;/span&gt; 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&amp;#8217;t anything new. Rails is nice enough to bundle a console script right within it!&lt;/p&gt;


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


	&lt;p&gt;So, if you aren&amp;#8217;t using it&amp;#8230; why not? got a moment? try this from the root path of your Rails application.&lt;/p&gt;


&lt;code&gt;
&lt;pre&gt;
./script/console
&lt;/pre&gt;
&lt;/code&gt;

It start up okay? If so, what is the name of one of your models? Let&amp;#8217;s say that I have a model structure like:
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="keyword"&gt;class &lt;/span&gt;&lt;span class="class"&gt;Customer&lt;/span&gt; &lt;span class="punct"&gt;&amp;lt;&lt;/span&gt; &lt;span class="constant"&gt;ActiveRecord&lt;/span&gt;&lt;span class="punct"&gt;::&lt;/span&gt;&lt;span class="constant"&gt;Base&lt;/span&gt;
  &lt;span class="ident"&gt;has_many&lt;/span&gt; &lt;span class="symbol"&gt;:orders&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="symbol"&gt;:dependent&lt;/span&gt; &lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="constant"&gt;true&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;

&lt;span class="keyword"&gt;class &lt;/span&gt;&lt;span class="class"&gt;Order&lt;/span&gt; &lt;span class="punct"&gt;&amp;lt;&lt;/span&gt; &lt;span class="constant"&gt;ActiveRecord&lt;/span&gt;&lt;span class="punct"&gt;::&lt;/span&gt;&lt;span class="constant"&gt;Base&lt;/span&gt;
  &lt;span class="ident"&gt;belongs_to&lt;/span&gt; &lt;span class="symbol"&gt;:customer&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

From &lt;code&gt;console&lt;/code&gt;, you can access your models and do all sorts of fun things.
&lt;code&gt;
&lt;pre&gt;
&amp;gt;&amp;gt; y = Customer.find(16)
=&amp;gt; #&amp;lt;Customer:0x2743ea4 @attributes={"name"=&amp;gt;"Robby", "id"=&amp;gt;"16"}&amp;gt;
&amp;gt;&amp;gt; y.orders
=&amp;gt; [#&amp;lt;Order:0x27416b8 @attributes={"id"=&amp;gt;"18", "amount"=&amp;gt;"12.00", "customer_id"=&amp;gt;"16"}&amp;gt;, #&amp;lt;Order:0x274167c @attributes={"id"=&amp;gt;"19", "amount"=&amp;gt;"12.50", "customer_id"=&amp;gt;"16"}&amp;gt;] 
&lt;/pre&gt;
&lt;/code&gt;

	&lt;p&gt;Pretty neat, huh?&lt;/p&gt;


&lt;code&gt;
&lt;pre&gt;
&amp;gt;&amp;gt; o = Order.find(18)
=&amp;gt; #&amp;lt;Order:0x273da68 @attributes={"id"=&amp;gt;"18", "amount"=&amp;gt;"12.00", "customer_id"=&amp;gt;"16"}&amp;gt;
&amp;gt;&amp;gt; o.customer.name
=&amp;gt; "Robby" 
&lt;/pre&gt;
&lt;/code&gt;

	&lt;p&gt;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.&lt;/p&gt;


	&lt;p&gt;It sure beats, hitting refresh in your browser all day. :-)&lt;/p&gt;
</description>
      <pubDate>Thu, 18 Aug 2005 22:06:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:f15997a9e5983273a9269fd0bf666eeb</guid>
      <author>Robby Russell</author>
      <link>http://www.robbyonrails.com/articles/2005/08/18/are-you-a-console-master</link>
      <category>Ruby on Rails</category>
      <category>Ruby</category>
      <category>Programming</category>
      <category>rails</category>
      <category>console</category>
      <category>activerecord</category>
    </item>
    <item>
      <title>"Are you a console master?" by Lance</title>
      <description>&lt;p&gt;Hi Robby,&lt;/p&gt;


	&lt;p&gt;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.&lt;/p&gt;


	&lt;p&gt;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.&lt;/p&gt;</description>
      <pubDate>Wed, 02 Aug 2006 06:58:30 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:1ceb4ca0-c681-436f-8f56-d91e15105bb0</guid>
      <link>http://www.robbyonrails.com/articles/2005/08/18/are-you-a-console-master#comment-21719</link>
    </item>
  </channel>
</rss>
