# Validates presence of foreign\_key & auto child save

**URL:** <https://www.ruby-forum.com/t/validates-presence-of-foreign-key-auto-child-save/147630>\
**Category:** Rails\
**Created:** [September 3, 2008, 7:57pm UTC](https://www.ruby-forum.com/t/validates-presence-of-foreign-key-auto-child-save/147630 "2008-09-03T19:57:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rasmus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7993a0/32.png) [@rasmus](https://www.ruby-forum.com/u/rasmus)\
**Post date:** [September 3, 2008, 7:57pm UTC](https://www.ruby-forum.com/t/validates-presence-of-foreign-key-auto-child-save/147630/1 "2008-09-03T19:57:40Z")

</div>

Hi there,

You so you know, I’m fairly new to rails - I still have a lot learn.

For fun I’m trying to create a forum in rails. I have some problems with  
the create method of the topic resource. I’ve set up a virtual attribute  
called text for topic, to use for mass assignment:

In topics\_controller.rb:  
@topic = Topic.new(params[:topic])

In topic.rb:  
def text=(text)  
posts.build(:text =\> text)  
end

The topic model handles the text-attributes by building a post model  
through its association.

This is pretty neat I think! It results in very readable and structured  
code. But when I try to save the topic with @topic.save it gives me the  
error that “Posts is invalid”. This is because post.rb includes the  
following:  
validates\_presence\_of :topic\_id

When the validation are run (just before the topic is actually saved)  
rails also validates the children of the topic - that is the post model.  
As this post has not yet defined a topic\_id its validations fail.

The topic id will not be available until the topic is created in the  
database - and the topic will not be created in the database until the  
post validates - and the post will not validate until the topic id is  
defined.

I hope you can see the cycle of dependencies.

If I just remove validates\_presence\_of :topic\_id it works like a charm.  
Rails even defines the posts topic id through the association set up  
with posts.build(). However, to me it seems like a “dirty” solution to  
the problem?

What would you do here? Any recommendations/ideas?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![rasmus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7993a0/32.png) [@rasmus](https://www.ruby-forum.com/u/rasmus)\
**Post date:** [September 4, 2008, 2:04am UTC](https://www.ruby-forum.com/t/validates-presence-of-foreign-key-auto-child-save/147630/2 "2008-09-04T02:04:47Z")

</div>

Rasmus N. wrote:

> error that “Posts is invalid”. This is because post.rb includes the  
> defined.  
> Thanks in advance.  
> The usual solution is:

class Topic  
def text=(text)  
# Rails should be setting the belongs-to object automatically here  
posts.build(:text =\> text, :topic =\> self)  
text # Make sure you return something that’s expected  
end  
end

class Post  
validates\_presence\_of :topic  
end

–  
Rails Wheels - Find Plugins, List & Sell Plugins -  
[http://railswheels.com](http://railswheels.com)
