# Confused

**URL:** <https://www.ruby-forum.com/t/confused/102125>\
**Category:** Rails\
**Created:** [January 5, 2006, 11:33pm UTC](https://www.ruby-forum.com/t/confused/102125 "2006-01-05T23:33:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jwigal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6a8cbe/32.png) [@jwigal](https://www.ruby-forum.com/u/jwigal)\
**Post date:** [January 5, 2006, 11:33pm UTC](https://www.ruby-forum.com/t/confused/102125/1 "2006-01-05T23:33:53Z")

</div>

Ok,

So, I’ve got two data tables associated through a foreign key.

class Child \< ActiveRecord::Base  
belongs\_to :parent, :class =\> Parent, :foreign\_key =\> “parent\_id”

# also has attr1, attr2

end

class Parent \< ActiveRecord::Base  
has\_many :children, :class =\> Child, :foreign\_key =\> “parent\_id”  
end

So, I’m trying to build the new/edit form for the child table…

Basically, I was aiming for something like:

\<%= text\_field “child”, “id”, :locked =\> “true” %\>  
  
\<%= text\_field “child.parent”, “id”, :locked =\> “true” %\>  
  
\<%= text\_field “child”, “attr1” %\>  
  
\<%= text\_field “child”, “attr2” %\>

but, that isn’t working … Can anybody enlighten me ?

j.

–  
“Remember. Understand. Believe. Yield! → [http://ruby-lang.org](http://ruby-lang.org)”

Jeff W.

---

<div class="post-metadata">

**Author:** ![jwigal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6a8cbe/32.png) [@jwigal](https://www.ruby-forum.com/u/jwigal)\
**Post date:** [January 5, 2006, 11:48pm UTC](https://www.ruby-forum.com/t/confused/102125/2 "2006-01-05T23:48:54Z")

</div>

It would be helpful to give more information about how it’s not working.

---

<div class="post-metadata">

**Author:** ![jwigal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6a8cbe/32.png) [@jwigal](https://www.ruby-forum.com/u/jwigal)\
**Post date:** [January 6, 2006, 2:19am UTC](https://www.ruby-forum.com/t/confused/102125/3 "2006-01-06T02:19:09Z")

</div>

Ah, more info …

Ok, so, I have a button on the parent detail page … to take me to  
entry for a new child. I’m passing the parents id as a parameter  
using

\<%= button\_to :action =\> ‘new’, :controller =\> ‘child’, :parent\_id =\>  
@parent[] %\>

So, the url to the new page is showing correctly:

[http://hostname](http://hostname):port/child/new?parent\_id=1

… so, in the new method for the child\_controller I do

@child = Child.new()  
@parent = Parent.find( params[:parent\_id] )  
@child.parent = @parent

… that seems to work.

so, that forms follows normal scaffolding and calls create on form  
submit.

I don’t understand how the attributes in create get set … and, what  
value I should be looking for when trying to save the association from  
Parent to Child.

Right now, that attribute just gets ignored and the record gets saved  
in the database with a null value for the parent id (in the child  
table).

… that’s where I’m stuck.

Thanks for your responses (past & future) & your time.

j.

On 1/5/06, Mike H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > # also has attr1, attr2
> > 
> > \<%= text\_field “child”, “id”, :locked =\> “true” %\>  
> >   
> > “Remember. Understand. Believe. Yield! → [http://ruby-lang.org](http://ruby-lang.org)”
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
“Remember. Understand. Believe. Yield! → [http://ruby-lang.org](http://ruby-lang.org)”

Jeff W.
