Assignment of parent ID in child's fk field


#1

When I create a child record I’m not automagically getting the parent ID
put into the fk field. Does object.new take parameters that allows me
to do that as part of the AR record creation? Or do I need to do it
separately? Or am I missing the boat altogether :wink:

Thanks,
Bill


#2

well if you create the child data first, you then need to associate it
to a
parent. For example if you have a page with many words

Then i think you can do something along the lines of

page = Page.new
page.words << Words.new(@params[:word])
page.save

I might be a little off here but I think thats the idea. If you post
some
code then maybe we can understand what you are doing wrong.

adam


#3

Hi Adam,

The parent already exists. I’m create the child, then update some of
its fields with the params returned from a form. I’ve included some of
the code below (shistory_item’s ‘emrec_id’ field is declared ‘not
null’) The app’s crashing on the ‘update_params’ call. I’m wondering
if I can just add a parameter in the private method call, or if I have
to change its logic to add a specific assignment of the emrec_id.

Thanks in advance for any help!
Bill


relevant code from the ‘update’ method in shistory_controller.rb


@emrec = find_emrec
@shistory_item = find_shistory

@shistory_item.update_attributes(params[:shistory_item])

and then, below…

private
def find_shistory
session[:shistory] ||= ShistoryItem.new
end

----- Original Message -----
From: Adam D.
To: removed_email_address@domain.invalid
Sent: 2006-03-11 12:15 PM
Subject: Re: [Rails] assignment of parent ID in child’s fk field

well if you create the child data first, you then need to associate it
to a parent. For example if you have a page with many words

Then i think you can do something along the lines of

page = Page.new
page.words << Words.new(@params[:word])
page.save

I might be a little off here but I think thats the idea. If you post
some code then maybe we can understand what you are doing wrong.

adam

On 3/11/06, Bill W. removed_email_address@domain.invalid wrote:
When I create a child record I’m not automagically getting the
parent ID put into the fk field. Does object.new take parameters that
allows me to do that as part of the AR record creation? Or do I need to
do it separately? Or am I missing the boat altogether :wink:

Thanks,
Bill

_______________________________________________
Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails