<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[Rails Forum - Ruby on Rails Help and Discussion Forum - Debugging on Rails: Reading Stack Traces]]></title>
		<link>http://railsforum.com/viewtopic.php?id=872</link>
		<description><![CDATA[The most recent posts in Debugging on Rails: Reading Stack Traces.]]></description>
		<lastBuildDate>Wed, 02 Nov 2011 07:01:44 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[Re: Debugging on Rails: Reading Stack Traces]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=146311#p146311</link>
			<description><![CDATA[<p>The brilliant tool valgrind is often able to say more about what is going wrong; it can give a strack trace to the point where things start to go wrong, which might be&nbsp; long time before the program actually crashes. Programs run through valgrind will run an order of magnitude slower and use more memory, but it will be buddyamazingly usable.</p>]]></description>
			<author><![CDATA[dummy@example.com (zuerst)]]></author>
			<pubDate>Wed, 02 Nov 2011 07:01:44 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=146311#p146311</guid>
		</item>
		<item>
			<title><![CDATA[Re: Debugging on Rails: Reading Stack Traces]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=4377#p4377</link>
			<description><![CDATA[<p>I remember the first time I saw one of those. I ended up with a full page error message over a typo and it freaked me out a little bit. I thought I&#039;d managed to really break something. I wasn&#039;t used to such thorough error messages. They can be intimidating to a new programmer, but very useful once you start reading them and following the path to the problem, particularly given the way a rails app is broken down.</p>]]></description>
			<author><![CDATA[dummy@example.com (Kelli)]]></author>
			<pubDate>Thu, 12 Oct 2006 19:52:37 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=4377#p4377</guid>
		</item>
		<item>
			<title><![CDATA[Debugging on Rails: Reading Stack Traces]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=4370#p4370</link>
			<description><![CDATA[<p><strong>What is a Stack Trace?</strong></p><p>A stack trace is that thing Rails spits out when there is a problem in the code. It might look something like this:</p><p><pre name="code" class="ruby:nogutter">/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1506:in `attributes=&#039;<br />/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1354:in `initialize_without_callbacks&#039;<br />/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/callbacks.rb:236:in `initialize&#039;<br />#{RAILS_ROOT}/app/controllers/projects_controller.rb:11:in `new&#039;</pre><br />No doubt about it, this looks scary. But, stack traces are very useful and shouldn&#039;t be ignored! They will help you find the problem in your code.</p><p>At some point, whether it be in your code or in the rails framework itself, an exception is raised (an error occurred). The stack trace shows exactly where the error occurred and the path it took to get there.</p><br /><p><strong>Reading the Stack Trace</strong></p><p>Take a look at the top line:</p><p><pre name="code" class="ruby:nogutter">/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1506:in `attributes=&#039;</pre><br />This tells you three things (from left to right):<br />1. The file the error occurred in<br />2. The line in the file the error occurred<br />3. The method the error occurred in.</p><p>This is what each one is in the example:</p><p><pre name="code" class="ruby:nogutter">File: /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb<br />Line: 1506<br />Method: attributes=</pre><br />The next line down in the stack trace represents where that method (attributes=) was called. As you can see it was called inside the <strong>initialize_without_callbacks</strong> method in the same file. If we keep going down we can see the path that was taken to get to the error.</p><br /><p><strong>A Simple Example</strong></p><p>Let me show you another example, but this time a little simpler. I typed this in irb:</p><p><pre name="code" class="ruby:nogutter">&gt;&gt; def foo<br />&gt;&gt;&nbsp; &nbsp;bar<br />&gt;&gt; end<br />&gt;&gt;<br />&gt;&gt; def bar<br />&gt;&gt;&nbsp; &nbsp;raise &#039;test&#039;<br />&gt;&gt; end<br />&gt;&gt;<br />&gt;&gt; foo<br />RuntimeError: test<br />&nbsp; &nbsp; &nbsp; &nbsp; from (irb):5:in `bar&#039;<br />&nbsp; &nbsp; &nbsp; &nbsp; from (irb):2:in `foo&#039;<br />&nbsp; &nbsp; &nbsp; &nbsp; from (irb):7<br />&nbsp; &nbsp; &nbsp; &nbsp; from :0</pre><br />Here we have two methods: <strong>foo</strong> and <strong>bar</strong>. The <strong>foo</strong> method calls <strong>bar</strong>, and <strong>bar</strong> raises an exception. When we call <strong>foo</strong>, we get the error and the stack trace below it.</p><p>Looking at the first line of the stack trace, we see the exception was raised in the &#039;bar&#039; method on line 5. The next line down shows that the <strong>foo</strong> method called the <strong>bar</strong> method on line 2.</p><br /><p><strong>Finding the Problem</strong></p><p>Now that we know how to read a stack trace, let&#039;s go back to the first example and find the problem.</p><p><pre name="code" class="ruby:nogutter">/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1506:in `attributes=&#039;<br />/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1354:in `initialize_without_callbacks&#039;<br />/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/callbacks.rb:236:in `initialize&#039;<br />#{RAILS_ROOT}/app/controllers/projects_controller.rb:11:in `new&#039;</pre><br />In this case the exception was raised inside a method in the Rails framework. This is why the file path looks so strange on the first few lines. If we wanted, we can go looking at the active_record/base.rb file it occurred in, but Rails is well tested, and it is more likely the problem exists in our own code.</p><p>If we keep going down the stack trace, we see a method was called in our code, specifically the &quot;new&quot; action in the projects_controller.rb. Now we are getting somewhere. I&#039;m guessing I&#039;m passing some value that Rails doesn&#039;t expect. Let&#039;s take a look at that method:</p><p><pre name="code" class="ruby:nogutter">def new<br />&nbsp; @project = Project.new(params[:id])<br />&nbsp; @task = Task.new<br />end</pre><br />Oh what a stupid mistake I made. For some reason I&#039;m passing params[:id] when creating the Project. I don&#039;t know what I was thinking, let me remove that:</p><p><pre name="code" class="ruby:nogutter">def new<br />&nbsp; @project = Project.new<br />&nbsp; @task = Task.new<br />end</pre><br />There we go, no more error! We could have probably found this easy enough without using the stack trace, but it just helped us locate the problem faster. On those tricky to find bugs, the stack trace will become all that much more useful!</p><br /><p><strong>TextMate Tip</strong></p><p>Here&#039;s a tip. If you&#039;re using TextMate, go grab the <a href="http://blog.inquirylabs.com/2006/09/28/textmate-footnotes-v16-released/">TextMate Footnotes</a> plugin. It will not only give you useful footnotes on each page, but it will also link each line of the stack trace that rails generates. This means you can click the line in the stack trace and the file will open instantly in TextMate, going to the appropriate line in the file. Very useful!</p>]]></description>
			<author><![CDATA[dummy@example.com (ryanb)]]></author>
			<pubDate>Thu, 12 Oct 2006 17:57:52 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=4370#p4370</guid>
		</item>
	</channel>
</rss>
