tags:

views:

30

answers:

1

I have there domain classes:

:: Person. (Person.ID, Name,Address) :: Designation.(Designation.ID, Title, Band) :: SalarySlip (Person.ID, Designation.ID, totalIncome, Tax etc etc.)

In the update method the person controller when I assign a person a designation from a list of designation values I want to insert a new record inside SalarySlip.

Something like:

def update = {
   def SalarySlipInstance = new SalarySlip()
   SalarySlipInstance.Person.ID = Params.ID //is this correct?
   SalarySlipInstance.Designation.ID = ?? //since the value is coming from a list. How can I bind this field?
}

Much Appreciated,

Thanks,
WB

+2  A: 

You need to load the Person and Designation objects first:

salarySlipInstance.Person = Person.get(params.person.id)
salarySlipInstance.Designation = Designation.get(params.designation.id)

If in your form you prefix the person and designation id's with person. and designation. it makes it easier to load.

Dave
Hi Dave,Thanks for your reply, I tried your solution but I am getting the following error:Cannot set property 'id' on null object
WaZ
the left hand side is wrong, try these: salarySlipInstance.Person = Person.get(params.person.id)salarySlipInstance.Designation = Designation.get(params.designation.id)
Emyr
I wrote this def SalarySlipInstance = new SalarySlip() SalarySlipInstance.person= Person.get(params.person.id) SalarySlipInstance.designation = Designation.get(params.designation .id) SalarySlipInstance.save() The update method works fine, however, when I check the data using dbPlugin, there is no data in the table!Am i missing anything here,Thanks.
WaZ
Apologies for the typo, rectified now. If it's not saved check the object binding errors (salarySlipInstance.hasErrors() salarySlipInstance.errors)
Dave
Thanks Dave and Emyr, worked like a charm.
WaZ