<?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: Tag console</title>
    <link>http://www.robbyonrails.com/articles/tag/console</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>thoughts.sort_by{|t| t[:topic]}.collect </description>
    <item>
      <title>Fixing PostgreSQL corruption with Rails?</title>
      <description>&lt;p&gt;People have been emailing me to let me know that the search on my blog was broken. Today I finally set out to fix the problem, which looked like a complex issue with my PostgreSQL database. I&amp;#8217;m not sure how long ago it started so I am not sure what was the cause at the moment. If you did a search on my blog you&amp;#8217;d get an application error and behind the scenes, you would see the following error.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;PGError: ERROR:  missing chunk number 0 for toast value 58441&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Eek!&lt;/p&gt;


	&lt;p&gt;So, I tried to &lt;span class="caps"&gt;REINDEX&lt;/span&gt; the table and it didn&amp;#8217;t solve the problem&amp;#8230; so I started poking around with different types of queries to see what I could do to generate the error again. Didn&amp;#8217;t take me long to figure out that it had something to do with the body column in the contents table.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
db_name=# SELECT count(id) FROM contents WHERE body ~* 'postgresql' LIMIT 20;
ERROR:  missing chunk number 0 for toast value 58441

db_name_=# SELECT count(id) FROM contents WHERE excerpt ~* 'postgresql' LIMIT 20;
 count 
-------
     0
(1 row)
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;What am I to do? I did some googling (and go figure&amp;#8230; the error being returned was caught on &lt;a href="http://www.google.com/search?&amp;#38;q=missing+chunk+number+0+for+toast+value"&gt;my blog by google&lt;/a&gt;)... which was amusing.&lt;/p&gt;


	&lt;p&gt;In the &lt;code&gt;#postgresql&lt;/code&gt; channel on freenode they recommended that I try and find the specific row in the table that was causing this error. I decided to just run a for loop in &lt;code&gt;script/console&lt;/code&gt; on the Content object in Typo and print out the name of each record until it gave me an error.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
for i in 1..30000
  puts Content.find( i ).title
  i =+ 1
end
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;This began to print out titles of older blog entries and sure enough&amp;#8230; the loop died when it hit the following error. :-)&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
  PostgreSQL sequences in Rails
  When TSearch2 Met AJAX
  ActiveRecord::StatementInvalid: PGError: ERROR:  missing chunk number 0 for toast value 58441
  : SELECT * FROM contents WHERE (contents.id = 1678)  LIMIT 1
          from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.1/lib/active_record/connection_adapters/abstract_adapter.rb:88:in `log'
          from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.1/lib/active_record/connection_adapters/postgresql_adapter.rb:137:in `execute'
          from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.1/lib/active_record/connection_adapters/postgresql_adapter.rb:351:in `select'
          from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.1/lib/active_record/connection_adapters/postgresql_adapter.rb:118:in `select_all'
          from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.1/lib/active_record/base.rb:431:in `find_by_sql'
          from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.1/lib/active_record/base.rb:395:in `find'
          from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.1/lib/active_record/base.rb:393:in `find'
          from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.1/lib/active_record/base.rb:409:in `find'
          from (irb):23
          from (irb):22
  &amp;gt;&amp;gt; exit
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;15.seconds.later I logged into psql and ran &lt;code&gt;DELETE FROM contents WHERE id = 1678&lt;/code&gt;... and all is well!&lt;/p&gt;
</description>
      <pubDate>Sat, 05 Aug 2006 19:16:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:378f2847-e095-4a05-931e-94fce37c0914</guid>
      <author>Robby Russell</author>
      <link>http://www.robbyonrails.com/articles/2006/08/05/fixing-postgresql-corruption-with-rails</link>
      <category>console</category>
      <category>corruption</category>
      <category>database</category>
    </item>
    <item>
      <title>irb history hack</title>
      <description>&lt;p&gt;Okay, I&amp;#8217;ve posted a &lt;a href="http://www.robbyonrails.com/articles/2005/10/01/master-console-and-servant"&gt;few&lt;/a&gt; &lt;a href="http://www.robbyonrails.com/articles/2005/10/03/master-console-and-servant-part-two"&gt;things&lt;/a&gt; about &lt;code&gt;console&lt;/code&gt; recently and I know that you&amp;#8217;re all using it&amp;#8230; right?&lt;/p&gt;


	&lt;p&gt;Well, &lt;a href="http://bitsweat.net/"&gt;Jeremy Kemper&lt;/a&gt; (bitsweat) mentioned at the &lt;a href="http://www.pdxruby.org"&gt;&lt;span class="caps"&gt;PDX&lt;/span&gt;.rb&lt;/a&gt; meeting last night that there was a way to keep history of your &lt;code&gt;irb&lt;/code&gt; commands. (more than just readline&amp;#8230; up-arrow&amp;#8217;n).&lt;/p&gt;


	&lt;p&gt;You can actually quit &lt;code&gt;irb&lt;/code&gt;, restart it and still see your history. This makes Robby happy.&lt;/p&gt;


	&lt;p&gt;&lt;a href="http://www.rubygarden.org/ruby?Irb/TipsAndTricks"&gt;See here for details&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;Thanks Jeremy!&lt;/p&gt;
</description>
      <pubDate>Wed, 05 Oct 2005 13:43:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:3157accd010e5df6005d70cf61545847</guid>
      <author>Robby Russell</author>
      <link>http://www.robbyonrails.com/articles/2005/10/05/irb-history-hack</link>
      <category>Ruby on Rails</category>
      <category>Ruby</category>
      <category>Programming</category>
      <category>ruby</category>
      <category>rails</category>
      <category>console</category>
      <category>irb</category>
    </item>
    <item>
      <title>Master, console, and servant part two</title>
      <description>&lt;p&gt;This is a brief addition to the post from a few days ago, &lt;a href="http://www.robbyonrails.com/articles/2005/10/01/master-console-and-servant"&gt;Master, console, and servant.&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;Noradio on &lt;span class="caps"&gt;IRC&lt;/span&gt; pointed out one thing that I did not mention in my previous blog entry&amp;#8230; that you can bind a subsession of &lt;code&gt;irb&lt;/code&gt; to a specific object. What??&lt;/p&gt;


	&lt;p&gt;Okay, let me explain this a bit more. Instead of always setting a variable to hold an instance of an object, you can be a bit more creative with &lt;code&gt;irb&lt;/code&gt;.&lt;/p&gt;


	&lt;p&gt;I present&amp;#8230;&lt;/p&gt;


	&lt;p&gt;&lt;img src="http://www.planetargon.com/files/~robby/console_irb_bind_objects.jpg" alt="" /&gt;&lt;/p&gt;


	&lt;p&gt;So, for example. You can perform the following in &lt;code&gt;script/console&lt;/code&gt;.&lt;/p&gt;


&lt;typo:code lang="ruby"&gt;
irb Model.find(4)
name
&lt;/type:code&gt;

	&lt;p&gt;If there is an attribute/method called name in your model, then it will call it there.  See my screen shot for some more details of how I used the &lt;code&gt;jobs&lt;/code&gt; and &lt;code&gt;fg #&lt;/code&gt; commands to switch jobs&amp;#8230;. and I even found out what happens when you attempt to switch to the current thread. :-)&lt;/p&gt;


	&lt;p&gt;Until next time&amp;#8230;&lt;/p&gt;
</description>
      <pubDate>Mon, 03 Oct 2005 23:54:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:9935c416cdd3d27d45838e651ead24ab</guid>
      <author>Robby Russell</author>
      <link>http://www.robbyonrails.com/articles/2005/10/03/master-console-and-servant-part-two</link>
      <category>Ruby on Rails</category>
      <category>Ruby</category>
      <category>Programming</category>
      <category>rails</category>
      <category>ruby</category>
      <category>console</category>
    </item>
    <item>
      <title>Master, console, and servant </title>
      <description>&lt;p&gt;I love console in Rails. It is absolutely one of my favorite tools in Rails. I probably spend more time testing stuff in console than I do in a browser, which is a good thing. Right?&lt;/p&gt;


	&lt;p&gt;Today, I was reading through a part of &lt;a href="http://pragmaticprogrammer.com/titles/ruby/index.html"&gt;Programming Ruby&lt;/a&gt; (I like to open up to a random page and read something every once in a while) and came across a section on page 189. &lt;strong&gt;Subsessions and Bindings&lt;/strong&gt; I was not aware that I could do this within &lt;strong&gt;irb&lt;/strong&gt; and immediately thought to myself, &amp;#8220;can I do that with console?&amp;#8221; Console is afterall&amp;#8230;just irb with all the Rails features pre-configured. So, I decided that I would try this out the next time I was in console.&lt;/p&gt;


	&lt;p&gt;That time came and passed&amp;#8230; so,  does it work?&lt;/p&gt;


	&lt;p&gt;The answer: &lt;strong&gt;&lt;span class="caps"&gt;YES&lt;/span&gt;!&lt;/strong&gt;&lt;/p&gt;


	&lt;p&gt;I even have proof&amp;#8230; :-)&lt;/p&gt;


	&lt;p&gt;&lt;img src="http://www.robbyrussell.com/albums/Desktops/console_subsessions.jpg" alt="" /&gt;&lt;/p&gt;


	&lt;p&gt;This should eliminate a few tabs in &lt;a href="http://iterm.sourceforge.net/"&gt;iterm&lt;/a&gt; for me and works just like the Unix &lt;code&gt;jobs&lt;/code&gt; and &lt;code&gt;fg&lt;/code&gt; commands do.&lt;/p&gt;


	&lt;p&gt;If you are still not &lt;a href="http://www.robbyonrails.com/articles/2005/08/18/are-you-a-console-master"&gt;using console&lt;/a&gt; yet, why?&lt;/p&gt;


	&lt;p&gt;Perhaps you enjoy the pain of constant browser reloads because you keep trying to figure out if that method works. Often times, you can copy and paste your code right into console. It&amp;#8217;s beautiful and gives you a quicker response than your browser will ever. 
But, you already knew that. Right?&lt;/p&gt;


	&lt;p&gt;Don&amp;#8217;t get me wrong, I can totally understand your justifications for the extra pain. Who wants to work in their terminal more than they have to? (I do&amp;#8230; but I do it because I am at home in my terminal.) There is nothing pretty about the terminal&amp;#8230; but I feel in control. Are you a servant of your pretty &lt;span class="caps"&gt;GUI IDE&lt;/span&gt; and browser or are you the master of your terminal? ;-)&lt;/p&gt;


	&lt;p&gt;Okay, I will end this nonsense with my new console marketing campaign. I have employeed &lt;a href="http://www.metropolisthegallery.com/images/Elkins_three_pigeons.png"&gt;three pigeons&lt;/a&gt; to help me work on this one and the end result of our hard work?&lt;/p&gt;


	&lt;p&gt;&lt;em&gt;Have you &lt;strong&gt;ruby script/console&lt;/strong&gt;&amp;#8216;d today?&lt;/em&gt;&lt;/p&gt;


	&lt;p&gt;Yes. Classy and Original. :-)&lt;/p&gt;


	&lt;p&gt;Back to writing&amp;#8230;&lt;/p&gt;
</description>
      <pubDate>Sat, 01 Oct 2005 20:47:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:6c302d4b116d870ff8a2fc85603991bc</guid>
      <author>Robby Russell</author>
      <link>http://www.robbyonrails.com/articles/2005/10/01/master-console-and-servant</link>
      <category>Ruby on Rails</category>
      <category>Ruby</category>
      <category>Programming</category>
      <category>ruby</category>
      <category>rails</category>
      <category>console</category>
      <category>programming</category>
    </item>
    <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>
  </channel>
</rss>
