Forum: Ruby on Rails trouble creating new records

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.
2b891e820c238ded365d035771603f21?d=identicon&s=25 Bill Walton (Guest)
on 2006-05-10 22:39
(Received via mailing list)
Is it possible to create / save a new record from a controller other
than the one I specified when I created the scaffolding?

My situation is that I have two tables.  Emrecs is the parent.  Actors
is the child and has a foreign key named emrec_id that references
emrec.id.  I scaffolded both, creating controllers and views for each.
My app collects info from the user to fill in the two tables, then
writes the info collected out in an XML file.

Now I need to read in the XML file and put the data back into the
tables.

Any suggestions?  Right now it looks like I'm going to have to open a
new REXML doc in each controller.  Is that right?

Thanks in advance!

Bill
D0cd6b10e01bacb976b3b815a9c660bc?d=identicon&s=25 Alex Wayne (Guest)
on 2006-05-11 02:09
Bill Walton wrote:
> Is it possible to create / save a new record from a controller other
> than the one I specified when I created the scaffolding?

Absolutely, from any controller anywhere in your app you can simply do

emrec = Emrec.new(:some_param => 'foo')
emrec.actors << Actor.new(:another_attribute => 'bar')
emrec.save

> My situation is that I have two tables.  Emrecs is the parent.  Actors
> is the child and has a foreign key named emrec_id that references
> emrec.id.  I scaffolded both, creating controllers and views for each.
> My app collects info from the user to fill in the two tables, then
> writes the info collected out in an XML file.
>
> Now I need to read in the XML file and put the data back into the
> tables.
>
> Any suggestions?  Right now it looks like I'm going to have to open a
> new REXML doc in each controller.  Is that right?

You can abstract any common code to a private method in
app/controllers/application.rb.  All controllers inherit from
ApplicationController so any methods you want shared between multiple
controllers should go here.
2b891e820c238ded365d035771603f21?d=identicon&s=25 Bill Walton (Guest)
on 2006-05-11 04:33
(Received via mailing list)
Hi Alex,

Thanks for your reply.  It confirms what I thought was the case.  It
turns
out I'd made a couple of (compounding) stupid-newbie mistakes.  1)  I
wasn't
populating a field I was validating (so of course the save didn't happen
;-p ) and 2) I was using a created (as opposed to scaffolded) view and
hadn't put <%= error_messages_for 'table_name' %> in it so I wasn't
seeing
the error messages that would have steered me to a solution without
wasting
bandwidth on the list.  Sorry for the noise.  But thanks again for your
reply.

Best regards,
Bill

----- Original Message -----
From: "Alex Wayne" <rubyonrails@beautifulpixel.com>
To: <rails@lists.rubyonrails.org>
Sent: Wednesday, May 10, 2006 7:09 PM
Subject: [Rails] Re: trouble creating new records
This topic is locked and can not be replied to.