<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Did You Know? Validating belongs_to Relationships</title>
	<atom:link href="http://shifteleven.com/articles/2006/11/08/did-you-know-validating-belongs_to-relationships/feed" rel="self" type="application/rss+xml" />
	<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>
	<description></description>
	<lastBuildDate>Thu, 26 Jan 2012 18:41:57 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Saint</title>
		<link>http://shifteleven.com/articles/2006/11/08/did-you-know-validating-belongs_to-relationships/comment-page-1#comment-10368</link>
		<dc:creator>Saint</dc:creator>
		<pubDate>Wed, 28 Jul 2010 00:08:16 +0000</pubDate>
		<guid isPermaLink="false">http://dev.fecalrod.com/articles/2006/11/08/did-you-know-validating-belongs_to-relationships#comment-10368</guid>
		<description>This will only work in cases where the Article association has not been pre-loaded. E.g.

comment = Comment.new

comment.article_id = Article.first.id

c.valid? #returns true

comment.article_id == comment.article.id #returns true


#So far so good

comment.article_id = nil

c.valid? #Still returns true because the article association is not reloaded for the validation

comment.article_id == comment.article.id #returns false. At this point the comment is valid and a save will succeed but the article_id saved in the db will be null. Perhaps the only way to handle this is to override the article_id= method and force Rails to reload the association:

  def article_id=(new_id)
    result = super(new_id)
    article(true)
    result
  end
  
Frankly Rails should be handling all for us this anyway.</description>
		<content:encoded><![CDATA[<p>This will only work in cases where the Article association has not been pre-loaded. E.g.</p>
<p>comment = Comment.new</p>
<p>comment.article_id = Article.first.id</p>
<p>c.valid? #returns true</p>
<p>comment.article_id == comment.article.id #returns true</p>
<p>#So far so good</p>
<p>comment.article_id = nil</p>
<p>c.valid? #Still returns true because the article association is not reloaded for the validation</p>
<p>comment.article_id == comment.article.id #returns false. At this point the comment is valid and a save will succeed but the article_id saved in the db will be null. Perhaps the only way to handle this is to override the article_id= method and force Rails to reload the association:</p>
<p>  def article_id=(new_id)<br />
    result = super(new_id)<br />
    article(true)<br />
    result<br />
  end</p>
<p>Frankly Rails should be handling all for us this anyway.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: BBG</title>
		<link>http://shifteleven.com/articles/2006/11/08/did-you-know-validating-belongs_to-relationships/comment-page-1#comment-9179</link>
		<dc:creator>BBG</dc:creator>
		<pubDate>Fri, 25 Jun 2010 00:38:02 +0000</pubDate>
		<guid isPermaLink="false">http://dev.fecalrod.com/articles/2006/11/08/did-you-know-validating-belongs_to-relationships#comment-9179</guid>
		<description>Great post. I really appreciate the information.  You are doing a great job communicating your message.  Keep up the good work.</description>
		<content:encoded><![CDATA[<p>Great post. I really appreciate the information.  You are doing a great job communicating your message.  Keep up the good work.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Raoul Duke</title>
		<link>http://shifteleven.com/articles/2006/11/08/did-you-know-validating-belongs_to-relationships/comment-page-1#comment-449</link>
		<dc:creator>Raoul Duke</dc:creator>
		<pubDate>Wed, 11 Feb 2009 16:53:57 +0000</pubDate>
		<guid isPermaLink="false">http://dev.fecalrod.com/articles/2006/11/08/did-you-know-validating-belongs_to-relationships#comment-449</guid>
		<description>@for some reason

AMEN!</description>
		<content:encoded><![CDATA[<p>@for some reason</p>
<p>AMEN!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Eric</title>
		<link>http://shifteleven.com/articles/2006/11/08/did-you-know-validating-belongs_to-relationships/comment-page-1#comment-21</link>
		<dc:creator>Eric</dc:creator>
		<pubDate>Wed, 12 Nov 2008 07:57:17 +0000</pubDate>
		<guid isPermaLink="false">http://dev.fecalrod.com/articles/2006/11/08/did-you-know-validating-belongs_to-relationships#comment-21</guid>
		<description>Thank you! I&#039;m learning RoR and found this very useful. For some reason, it can be difficult finding this kind of information.</description>
		<content:encoded><![CDATA[<p>Thank you! I&#8217;m learning RoR and found this very useful. For some reason, it can be difficult finding this kind of information.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

