# Parent child relations

**URL:** <https://www.ruby-forum.com/t/parent-child-relations/83837>\
**Category:** Rails\
**Created:** [January 30, 2007, 5:36am UTC](https://www.ruby-forum.com/t/parent-child-relations/83837 "2007-01-30T05:36:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrisolsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c6cbf5/32.png) [@chrisolsen](https://www.ruby-forum.com/u/chrisolsen)\
**Post date:** [January 30, 2007, 5:36am UTC](https://www.ruby-forum.com/t/parent-child-relations/83837/1 "2007-01-30T05:36:04Z")

</div>

I know this is a silly question, but I am just wondering when making the  
link between children and the parent table, for example a blog reply, is  
there a “best practice” way of doing this.

The only way that I can think of is to create a hidden input field  
within   
ex, (of course do it the  
ruby way).

Is this the only way?

Thanks for the help

\<% form\_for :url =\> {:action =\> “create\_reply”} %\>  
\<%= form.hidden\_field :post\_id %\>  
\<%= form.text\_field :title, :size =\> 30 %\>  
\<%= form.text\_area :content, :cols =\> 40, :rows =\> 5 %.\>  
\<%= submit\_tag “Post”, :class =\> “submit” %\>  
\<% end %\>

---

<div class="post-metadata">

**Author:** ![chrisolsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c6cbf5/32.png) [@chrisolsen](https://www.ruby-forum.com/u/chrisolsen)\
**Post date:** [January 30, 2007, 5:57am UTC](https://www.ruby-forum.com/t/parent-child-relations/83837/2 "2007-01-30T05:57:56Z")

</div>

Normally, one would use:

\<% form\_for :url =\> {:action =\> “create\_reply”, :id =\> @posts} %\>

This generates something like:

So, the actual post id is buried in the url to which you will issue your  
POST request.

–steve

Keith L.-2 wrote:

> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
View this message in context:  
[http://www.nabble.com/-Rails--Parent-child-relations-tf3140275.html#a8703916](http://www.nabble.com/-Rails--Parent-child-relations-tf3140275.html#a8703916)  
Sent from the RubyOnRails Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![chrisolsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c6cbf5/32.png) [@chrisolsen](https://www.ruby-forum.com/u/chrisolsen)\
**Post date:** [January 30, 2007, 6:31am UTC](https://www.ruby-forum.com/t/parent-child-relations/83837/3 "2007-01-30T06:31:32Z")

</div>

Thanks for the reply. I thought that doing it that way embeds the :id  
value into the params hash table through the id key ex, params[:id]

Do you then have make the following setting manually?  
params[:post\_id] = params[:id]

Steve R. wrote:

> Normally, one would use:
> 
> \<% form\_for :url =\> {:action =\> “create\_reply”, :id =\> @posts} %\>
> 
> This generates something like:
> 
> So, the actual post id is buried in the url to which you will issue your  
> POST request.

---

<div class="post-metadata">

**Author:** ![chrisolsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c6cbf5/32.png) [@chrisolsen](https://www.ruby-forum.com/u/chrisolsen)\
**Post date:** [January 30, 2007, 9:26am UTC](https://www.ruby-forum.com/t/parent-child-relations/83837/4 "2007-01-30T09:26:52Z")

</div>

two ways to do what u want to do:

a) hidden field. you suggested this above.

b) via the controller when passing the id in :id =\> x ; but instead of

params[:post\_id] = params[:id]  
do  
@reply.post\_id = params[:id].to\_i # the to\_i isn’t really that  
neccessary

---

<div class="post-metadata">

**Author:** ![chrisolsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c6cbf5/32.png) [@chrisolsen](https://www.ruby-forum.com/u/chrisolsen)\
**Post date:** [January 30, 2007, 3:21pm UTC](https://www.ruby-forum.com/t/parent-child-relations/83837/5 "2007-01-30T15:21:15Z")

</div>

That make sense. I guess I thought rails would have it’s own way of  
doing this.

The only other thing that I am wondering about is whether the  
params[:id] value must be cleared out to prevent the save method from  
thinking that the attempted insert is actually an update. I will have  
to check this out after work.

Thanks for all the help.

\<% form\_for :reply, :url =\> {:action =\> ‘create\_reply’} do |form| %\>  
…  
\<% end %\>

////  
def create\_reply  
params[:post\_id] = params[:id]  
params[:id] = nil

post = Post.new(params[:reply])  
post.save  
end
