# Creating a new object with a passed in parent?

**URL:** <https://www.ruby-forum.com/t/creating-a-new-object-with-a-passed-in-parent/102009>\
**Category:** Rails\
**Created:** [February 7, 2006, 1:49pm UTC](https://www.ruby-forum.com/t/creating-a-new-object-with-a-passed-in-parent/102009 "2006-02-07T13:49:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Duane](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b4bc9f/32.png) [@Duane](https://www.ruby-forum.com/u/Duane)\
**Post date:** [February 7, 2006, 1:49pm UTC](https://www.ruby-forum.com/t/creating-a-new-object-with-a-passed-in-parent/102009/1 "2006-02-07T13:49:40Z")

</div>

I have a simple object, Article, which has\_one :parent of the same time.  
On my Show page for a given article there will be a “new child” button  
which will pass the current :id (or the current Article itself) to the  
new form. What is the proper way to hide a reference to this parent in  
the form so that when it is submitted back, the create method knows what  
to do with it?

I’m confused by where Rails is going to make assumptions for me. Does  
article have a property called parent, or parent\_id? When I set it do I  
just pass in an id and Rails knows what to do with it, or do I have to  
create the object myself and then set it? I tried something like this:

\<%= hidden\_field ‘article’, ‘parent’, @parent %\>

where @parent had been defined in the new method as  
@parent = Article.find(params[:id])

but I got undefined method `stringify\_keys’ for #Article:0x2544068

I’m guessing that’s because I’m trying to pass the object through the  
form rather than an id reference to it? But if I pass around just the  
id, I can’t really use the above hidden\_field reference to set  
article.parent, can I?

---

<div class="post-metadata">

**Author:** ![Duane](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b4bc9f/32.png) [@Duane](https://www.ruby-forum.com/u/Duane)\
**Post date:** [February 7, 2006, 1:53pm UTC](https://www.ruby-forum.com/t/creating-a-new-object-with-a-passed-in-parent/102009/2 "2006-02-07T13:53:25Z")

</div>

On Tuesday, February 07, 2006, at 1:49 PM, Duane wrote:

> I’m guessing that’s because I’m trying to pass the object through the  
> form rather than an id reference to it? But if I pass around just the  
> id, I can’t really use the above hidden\_field reference to set  
> article.parent, can I?

Sure! just do find the parent using the passed parent id like:

parent = Article.find(params[:parent\_id])  
…stuff goes here…  
new\_article.parent=parent

Mikkel B.

www.strongside.dk - Football Portal(DK)  
[nflfeed.helenius.org](http://nflfeed.helenius.org) - Football News(DK)  
ting.minline.dk - Buy Old Stuff!(DK)

---

<div class="post-metadata">

**Author:** ![Duane](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b4bc9f/32.png) [@Duane](https://www.ruby-forum.com/u/Duane)\
**Post date:** [February 7, 2006, 3:51pm UTC](https://www.ruby-forum.com/t/creating-a-new-object-with-a-passed-in-parent/102009/3 "2006-02-07T15:51:17Z")

</div>

Mikkel B. wrote:

> parent = Article.find(params[:parent\_id])  
> …stuff goes here…  
> new\_article.parent=parent

Thank you for the reply Mikkel, but I’m not sure I understand. Where  
does this code go? In my create method, which is receiving the results  
of the form submission? That would mean I had to pass back a parameter  
called parent\_id, right? What “stuff” are you referring to? Here’s a  
typical create method, via the scaffold generation:

def create  
@article = Article.new(params[:article])  
if @article.save  
flash[:notice] = ‘Article was successfully created.’  
redirect\_to :action =\> ‘list’  
else  
render :action =\> ‘new’  
end  
end

I assume that I would put your code before the save command, which is  
why I’m not sure what stuff might go in between those two statements.

And where did parent\_id come from? That’s the hidden field I put in my  
form, or is that an additional property that I add to the Article model?

Thank you!  
Duane

> Mikkel B.
> 
> www.strongside.dk - Football Portal(DK)  
> [nflfeed.helenius.org](http://nflfeed.helenius.org) - Football News(DK)  
> ting.minline.dk - Buy Old Stuff!(DK)

---

<div class="post-metadata">

**Author:** ![Duane](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b4bc9f/32.png) [@Duane](https://www.ruby-forum.com/u/Duane)\
**Post date:** [February 7, 2006, 3:59pm UTC](https://www.ruby-forum.com/t/creating-a-new-object-with-a-passed-in-parent/102009/4 "2006-02-07T15:59:36Z")

</div>

> I assume that I would put your code before the save command, which is  
> why I’m not sure what stuff might go in between those two statements.
> 
> And where did parent\_id come from? That’s the hidden field I put in my  
> form, or is that an additional property that I add to the Article model?

parent\_id could/should be the name of your hidden field.

And yes, “do stuff” should occur before your save…

Mikkel B.

www.strongside.dk - Football Portal(DK)  
[nflfeed.helenius.org](http://nflfeed.helenius.org) - Football News(DK)  
ting.minline.dk - Buy Old Stuff!(DK)
