Forum: Ruby on Rails Confused...

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Jeff W. (Guest)
on 2006-01-06 00:33
(Received via mailing list)

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" %><br/>
<%= text_field "child.parent", "id", :locked => "true" %><br/>
<%= text_field "child", "attr1" %><br/>
<%= text_field "child", "attr2" %><br/>

but, that isn't working ... Can anybody enlighten me ?


"Remember. Understand. Believe. Yield! ->"

Jeff W.
Mike H. (Guest)
on 2006-01-06 00:48
(Received via mailing list)
It would be helpful to give more information about how it's not working.
Jeff W. (Guest)
on 2006-01-06 03:19
(Received via mailing list)
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. <removed_email_address@domain.invalid> wrote:
> >  # also has attr1, attr2
> ><%= text_field "child", "id", :locked => "true" %><br/>
> >"Remember. Understand. Believe. Yield! ->"
> _______________________________________________
> Rails mailing list
> removed_email_address@domain.invalid

"Remember. Understand. Believe. Yield! ->"

Jeff W.
This topic is locked and can not be replied to.