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


class Parent < ActiveRecord::Base
has_many :children, :class => Child, :foreign_key => “parent_id”

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 ?


“Remember. Understand. Believe. Yield! ->

Jeff W.

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

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

<%= button_to :action => ‘new’, :controller => ‘child’, :parent_id =>
@parent[] %>

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


… so, in the new method for the child_controller I do

@child =
@parent = Parent.find( params[ :parent_id ] )
@child.parent = @parent

… that seems to work.

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

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

… that’s where I’m stuck.

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


On 1/5/06, Mike H. [email protected] wrote:

also has attr1, attr2

<%= text_field “child”, “id”, :locked => “true” %>

“Remember. Understand. Believe. Yield! ->

Rails mailing list
[email protected]

“Remember. Understand. Believe. Yield! ->

Jeff W.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs