# Validate associations

**URL:** <https://www.ruby-forum.com/t/validate-associations/179025>\
**Category:** Rails\
**Created:** [November 16, 2009, 2:04am UTC](https://www.ruby-forum.com/t/validate-associations/179025 "2009-11-16T02:04:43Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![introvert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/c37758/32.png) [@introvert](https://www.ruby-forum.com/u/introvert)\
**Post date:** [November 16, 2009, 2:04am UTC](https://www.ruby-forum.com/t/validate-associations/179025/1 "2009-11-16T02:04:43Z")

</div>

Hello,

I have a simple Review model (article has many reviews):

class Review \< ActiveRecord::Base  
belongs\_to :article

validates\_presence\_of :article\_id

validates\_associated :article  
end

However, if I set the article\_id to some random article id that doesnt  
exist, it wont show an error and will save the record to the database as  
it would normally do.

How do you do validate associations?

---

<div class="post-metadata">

**Author:** ![introvert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/c37758/32.png) [@introvert](https://www.ruby-forum.com/u/introvert)\
**Post date:** [November 16, 2009, 2:10pm UTC](https://www.ruby-forum.com/t/validate-associations/179025/2 "2009-11-16T14:10:57Z")

</div>

Hello Al, thank you for pointing this out.  
You are right, it seems to be intuitive that validates\_associated should  
check the existence of the associated object. But it doesn’t.  
Actually the problem is well known…

> **[Pragmatic Bookshelf: By Developers, For Developers](https://pragprog.com)**
>
> We improve the lives of professional developers. We create timely, practical books on classic and cutting-edge topics to help you learn and practice your craft, and accelerate your career. Come learn with us.

[http://dev.rubyonrails.org/ticket/5369](http://dev.rubyonrails.org/ticket/5369)  
[http://blog.hasmanythrough.com/2007/7/14/validate-your-existence](http://blog.hasmanythrough.com/2007/7/14/validate-your-existence)

…but running Rails 2.3.4 the misunderstanding does not appear to be  
addressed.

So, for example, following the hints you can see in the pages linked  
above, your models could be:

# review.rb

class Review \< ActiveRecord::Base  
belongs\_to :article  
validates\_presence\_of :article\_id  
validates\_associated :article  
validate :existence\_of\_article, :if =\> :article\_id?

def article\_id? # just not to bury the users with pointless error  
messages  
return true if article\_id and !article\_id.blank?  
end

def existence\_of\_article # this actually checks the existence of the  
object, called by validate  
errors.add(:article\_id, “must exist to save the review.”)   
unless article\_id? and article  
end  
end

#article.rb  
class Article \< ActiveRecord::Base  
has\_many :reviews # not involved in validation of the review, as far I  
can understand  
end

You could use the plugin, but I always do as shown above, because I  
prefer to have the least of dependencies on external components, and see  
what’s going on.

Hope this helps.

Luca B.

Al F. wrote:

> Hello,
> 
> I have a simple Review model (article has many reviews):
> 
> class Review \< ActiveRecord::Base  
> belongs\_to :article
> 
> validates\_presence\_of :article\_id
> 
> validates\_associated :article  
> end
> 
> However, if I set the article\_id to some random article id that doesnt  
> exist, it wont show an error and will save the record to the database as  
> it would normally do.
> 
> How do you do validate associations?
