<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>nick has a blog! &#187; rails</title>
	<atom:link href="http://www.nickpeters.net/category/rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.nickpeters.net</link>
	<description></description>
	<lastBuildDate>Thu, 16 Dec 2010 07:07:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.3</generator>
		<item>
		<title>Fix for &#8220;uninitialized constant Gem::GemRunner (NameError)&#8221;</title>
		<link>http://www.nickpeters.net/2007/12/31/fix-for-uninitialized-constant-gemgemrunner-nameerror/</link>
		<comments>http://www.nickpeters.net/2007/12/31/fix-for-uninitialized-constant-gemgemrunner-nameerror/#comments</comments>
		<pubDate>Mon, 31 Dec 2007 20:43:08 +0000</pubDate>
		<dc:creator>Nick</dc:creator>
				<category><![CDATA[fix]]></category>
		<category><![CDATA[mac]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://www.nickpeters.net/2007/12/31/fix-for-uninitialized-constant-gemgemrunner-nameerror/</guid>
		<description><![CDATA[I tried upgrading my version of rubygems to the most current version by running sudo gem update --system Which introduced this error: /usr/bin/gem:23: uninitialized constant Gem::GemRunner(NameError) whenever I tried to run rubygems. On the rails forum, I found a fix for it! Simply add the line to the file /usr/bin/gem (may be different on a [...]]]></description>
			<content:encoded><![CDATA[<p>I tried upgrading my version of rubygems to the most current version by running<br />
<code>sudo gem update --system</code><br />
Which introduced this error:<br />
<code>/usr/bin/gem:23: uninitialized constant Gem::GemRunner(NameError)</code><br />
whenever I tried to run rubygems.  On the <a href="http://railsforum.com/" target="_blank">rails forum</a>, I found <a href="http://railsforum.com/viewtopic.php?pid=48963">a fix for it!</a><br />
Simply add the line to the file /usr/bin/gem (may be different on a mac)<br />
<code>require 'rubygems/gem_runner'</code><br />
after<br />
<code>require 'rubygems'</code><br />
This fixed the issue in both Ubuntu and OS 10.4.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.nickpeters.net/2007/12/31/fix-for-uninitialized-constant-gemgemrunner-nameerror/feed/</wfw:commentRss>
		<slash:comments>100</slash:comments>
		</item>
		<item>
		<title>Delete Vs. Destroy In Rails</title>
		<link>http://www.nickpeters.net/2007/12/21/delete-vs-destroy/</link>
		<comments>http://www.nickpeters.net/2007/12/21/delete-vs-destroy/#comments</comments>
		<pubDate>Fri, 21 Dec 2007 20:07:24 +0000</pubDate>
		<dc:creator>Nick</dc:creator>
				<category><![CDATA[rails]]></category>
		<category><![CDATA[delete]]></category>
		<category><![CDATA[destroy]]></category>

		<guid isPermaLink="false">http://www.nickpeters.net/2007/12/21/delete-vs-destroy/</guid>
		<description><![CDATA[I&#8217;ve been writing this web app that has the following models: Order, Recipient, Message. An order has many recipients and a recipient has many messages. The recipients are also dependent upon the order and the messages are dependent upon the recipient. In Ruby code this looks like: Order.rb class Order &#60; ActiveRecord::Base has_many :recipients, :dependent [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been writing this web app that has the following models: Order, Recipient, Message.  An order has many recipients and a recipient has many messages.  The recipients are also dependent upon the order and the messages are dependent upon the recipient.</p>
<p>In Ruby code this looks like:<br />
Order.rb<br />
<code><br />
class Order &lt; ActiveRecord::Base<br />
has_many :recipients, :dependent =&gt; :destroy<br />
has_many :messages, :through =&gt; :recipients<br />
end<br />
</code><br />
Recipient.rb<br />
<code><br />
class Recipient &lt; ActiveRecord::Base<br />
belongs_to :order<br />
has_many :messages, :dependent =&gt; :destroy<br />
end<br />
</code><br />
The idea here is that when I delete an order, I also delete any associated recipients and any associated messages.  My controller looked like this:<br />
<code><br />
def delete<br />
Order.delete(params[:id])<br />
end<br />
</code></p>
<p>There are a couple things wrong with this.  The first and most important thing is that when I delete a row in the order table, it leaves orphaned rows in the order and messages table.  I want to delete these rows as well!  The next thing is that this isn&#8217;t <a href="http://railscasts.com/episodes/35">RESTful design</a>.  Instead of calling my method &#8220;delete,&#8221; let&#8217;s call it &#8220;destroy.&#8221;  To fix the issue though, we need to use another method that has subtle difference than delete.  Instead of delete, I should be using the destroy method.  Why is that?</p>
<p>The delete method essentially deletes a row (or an array of rows) from the database.  Destroy on the other hand allows for a few more options.  First, it will check any callbacks such as before_delete, or any dependencies that we specify in our model.  Next, it will keep the object that just got deleted in memory; this allows us to leave a message saying something like &#8220;Order #{order.id} has been deleted.&#8221;  Lastly, and most importantly, it will also delete any child objects associated with that object!</p>
<p>Now my code in my controller looks like this.<br />
<code><br />
def destroy<br />
Order.destroy(params[:id])<br />
end<br />
</code><br />
Whenever I delete an order object, it will delete all child recipient and message rows in the database .  This prevents any orphaned rows and allows for consistency in the database!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.nickpeters.net/2007/12/21/delete-vs-destroy/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

