How to update value of a field in one table from another table.

K

Kanaan

Guest
I'm working on a simple grails project and I was wondering if there is a way I can make changes to table and the changes are reflected in another table. I have several tables two of which are Inventory table and Movement table.

Inventory table stores information about assets such as User who owns the asset, while Movement table houses information about the movement of assets between users (e.g. when one user leaves the organization, the asset he owns is transferred to a new user). What I want to do is that if I enter a new record in the Movement table such as old_user and new_user, the change should be reflected in Inventory table as new_user.

How can I achieve that.??

Code:
package com.inventory

class Inventory {

	String inventoryID
	String code
	String description
	String serial_num
	Date purchase_date
	byte[] image
	Date record_date
	String remarks
	Type type 
	Brand brand 
	User user 


	static hasMany = [movements: Movement]
	//static mappedBy = [movementsByOldUser: 'oldUser', movementsByNewUser: 'newUser']

	String toString(){
		"$inventoryID, $type"
	}
	
    static constraints = {
    	inventoryID blank: false, unique: true
    	code blank: false
    	description nullable: true, maxSize: 1000
    	serial_num blank: false
    	purchase_date()
    	image nullable: true, maxSize: 1000000
    	record_date()
    	remarks nullable: true, maxSize: 1000
    	type()
    	brand()
    	user()
    }

    //def  beforeUpdate(){
    //	this.user = movementsByNewUser
    //}
}

Code:
package com.inventory

class Movement {

	User oldUser
	User newUser
	Inventory inventoryID
	Date movementDate
	User userResponsible
    String reason

	//static belongsTo = User


    static constraints = {
    	inventoryID blank: false
    	oldUser blank: false
    	newUser blank: false
    	movementDate()
    	userResponsible blank: false
        reason maxSize: 1000
    }
}

Code:
package com.inventory

class User {

	String userID
	String fullName
	String position
	Department department 

	String toString(){
		fullName
	}

	static hasMany = [inventories: Inventory, movementsByOldUser: Movement, movementsByNewUser: Movement]
	static mappedBy = [movementsByOldUser: 'oldUser', movementsByNewUser: 'newUser']
	 

	//def beforeInsert(){
	//	this.fullName = movementsByOldUser
	//}

	//def beforeUpdate(){
	//	this.fullName = movementsByNewUser
	//}

    static constraints = {
    	userID blank: false, unique: true
    	fullName blank: false
    	position()
    	department()
    }
}
 
Top