# Adding records to a child table from a parent table

**URL:** <https://www.ruby-forum.com/t/adding-records-to-a-child-table-from-a-parent-table/226007>\
**Category:** Rails\
**Created:** [May 9, 2008, 8:40am UTC](https://www.ruby-forum.com/t/adding-records-to-a-child-table-from-a-parent-table/226007 "2008-05-09T08:40:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![doodle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9de0a6/32.png) [@doodle](https://www.ruby-forum.com/u/doodle)\
**Post date:** [May 9, 2008, 8:40am UTC](https://www.ruby-forum.com/t/adding-records-to-a-child-table-from-a-parent-table/226007/1 "2008-05-09T08:40:40Z")

</div>

I have a table (ded\_masts) that contains all the deductions (and their  
parameters/characteristics) that a client (company) has instantiated/  
authorized - the “parent table”. Each employee of the company has  
records in the child table - deducts - that correspond to the  
authorized company-level deductions in ded\_masts. On recording a new  
employee, I need to query the ded\_masts table and record them in the  
deducts table and then allow editing.

In the legacy system, we query the ded\_masts table for all allowed  
deductions for that company, then iterate through the query creating  
and saving the employee level deduction records (it is NOT a trigger  
for several reasons).

In Rails, I’ve tried this in the deducts\_controller:  
def create\_newee\_deducts  
@dedmasts = DedMast.find(:all, :conditions =\> [“client\_id = ?”,  
params[:clientid]])  
for DedMast in @dedmasts  
@dname = @DedMast.long\_name  
@sname = @dname[0, 9]  
@deduct = Deduct.new  
@deduct.client\_id = params[:clientid]  
@deduct.ee\_id = params[:neweeid]  
@deduct.precedence = @DedMast.precedence  
@deduct.code = @DedMast.code  
@deduct.name = @sname  
@deduct.ach\_allowed = @DedMast.ach\_allowed  
@deduct.save  
end  
end

But I get various errors about loading constants and unexpected ends.

Is it not possible to access a model from another model’s controller?

---

<div class="post-metadata">

**Author:** ![doodle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9de0a6/32.png) [@doodle](https://www.ruby-forum.com/u/doodle)\
**Post date:** [May 9, 2008, 6:20pm UTC](https://www.ruby-forum.com/t/adding-records-to-a-child-table-from-a-parent-table/226007/2 "2008-05-09T18:20:51Z")

</div>

You can access models from _anywhere_. As in, if I have a model  
called Flange, I can call Flange.find(:all) from anywhere I want.

The errors about loading constants and “unepxected ends” points to  
syntax errors in the files Rails is trying to load for you.

The file causing the error is _likely_ going to be the one that  
contains the constant (probably a class or module) that it’s  
complaining about.

I think you need to return to some basics of the Ruby language before  
continuing though… some of your pasted code tells me that either you  
don’t really understand the basics of ruby (and ruby ‘style’) or  
you’ve littered your code with careless bugs.

DedMast is a class  
@deduct is an instance variable in your controller

@DedMast is … a disaster waiting to happen.

for DedMast in @dedmasts  
end

is just plain wrong. You’re telling ruby to create a variable called  
DedMast so that it can hold each @dedmast value during the loop.  
Except DedMast is a constant (your class) so that name is already  
taken.

If you don’t understand the basics of ruby variables and classes then  
you’re really going to be in a world of pain trying to get anything to  
work.

Take a day or so with the Pickaxe ruby book (google it) and learn the  
basics.

Regards,  
Trevor

On 5/8/08, doodle [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> deductions for that company, then iterate through the query creating  
> @deduct = Deduct.new  
> But I get various errors about loading constants and unexpected ends.
> 
> Is it not possible to access a model from another model’s controller?
> 
> >

## –

Trevor S.  
[http://somethinglearned.com](http://somethinglearned.com)
