Adding records to a child table from a parent table

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?

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 [email protected] 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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs