# Trouble creating new records

**URL:** <https://www.ruby-forum.com/t/trouble-creating-new-records/60178>\
**Category:** Rails\
**Created:** [May 10, 2006, 10:39pm UTC](https://www.ruby-forum.com/t/trouble-creating-new-records/60178 "2006-05-10T22:39:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [May 10, 2006, 10:39pm UTC](https://www.ruby-forum.com/t/trouble-creating-new-records/60178/1 "2006-05-10T22:39:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [May 11, 2006, 2:09am UTC](https://www.ruby-forum.com/t/trouble-creating-new-records/60178/2 "2006-05-11T02:09:28Z")

</div>

Bill W. 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.

---

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [May 11, 2006, 4:33am UTC](https://www.ruby-forum.com/t/trouble-creating-new-records/60178/3 "2006-05-11T04:33:57Z")

</div>

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 W.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Wednesday, May 10, 2006 7:09 PM  
Subject: [Rails] Re: trouble creating new records
