Forum: Ruby on Rails assignment of parent ID in child's fk field

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Bill W. (Guest)
on 2006-03-11 19:53
(Received via mailing list)
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 ;-)

Thanks,
Bill
Adam D. (Guest)
on 2006-03-11 20:15
(Received via mailing list)
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
Bill W. (Guest)
on 2006-03-11 22:12
(Received via mailing list)
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 ;-)

    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
This topic is locked and can not be replied to.