<?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>Blog - ShiftEleven &#187; belongs_to</title>
	<atom:link href="http://shifteleven.com/articles/tag/belongs_to/feed" rel="self" type="application/rss+xml" />
	<link>http://shifteleven.com</link>
	<description></description>
	<lastBuildDate>Mon, 09 Jan 2012 04:04:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Did You Know? Validating belongs_to Relationships</title>
		<link>http://shifteleven.com/articles/2006/11/08/did-you-know-validating-belongs_to-relationships?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=did-you-know-validating-belongs_to-relationships</link>
		<comments>http://shifteleven.com/articles/2006/11/08/did-you-know-validating-belongs_to-relationships#comments</comments>
		<pubDate>Thu, 09 Nov 2006 01:41:52 +0000</pubDate>
		<dc:creator>K. Adam Christensen</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[belongs_to]]></category>
		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://dev.fecalrod.com/articles/2006/11/08/did-you-know-validating-belongs_to-relationships</guid>
		<description><![CDATA[This is one of those things that is probably obvious to some, and just not known to some. I was in that latter description up until a few minutes ago. It&#8217;s that you can easily validate any belongs_to relationship by &#8230; <a href="http://shifteleven.com/articles/2006/11/08/did-you-know-validating-belongs_to-relationships">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>This is one of those things that is probably obvious to some, and just not known to some.  I was in that latter description up until a few minutes ago.  It&#8217;s that you can easily validate any <code>belongs_to</code> relationship by simply using <code>valdiates_presence_of</code></p>
<p>.</p>
<pre class="ruby" title="code">class Comment &lt; ActiveRecord::Base
  belongs_to :article

  validates_presence_of :article
end</pre>
<p>If you instantiate a new <code>Comment</code>, and don&#8217;t assign an <code>Article</code> to the comment instance, the record won&#8217;t save.  I have to say, that seemed pretty obvious.  But there is another interesting piece to this.  Say I passed the <code>article_id</code> in the <code>params</code> and tried to assign that to our instance</p>
<pre class="ruby" title="code">comment = Comment.new
comment.article_id = params[:article_id]</pre>
<p>If <code>article_id</code> corresponds to a real article in our database, then <code>comment.article</code> will have be assigned an <code>Article</code> with that <code>id</code>.  Now if <code>params[:article_id]</code> happens to be some id which doesn&#8217;t exist, like <code>99999999999</code>, then <code>comment.article</code> will be <code>nil</code> and thus the record fails validation.  This means that you don&#8217;t have to test for the existence of the <code>Article</code> against your records because rails will do this for you when it checks to see if <code>comment.article</code> is nil or not.</p>
]]></content:encoded>
			<wfw:commentRss>http://shifteleven.com/articles/2006/11/08/did-you-know-validating-belongs_to-relationships/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

