0
votes

below is the domain class structured

class User {
   String username
   String password
   String emailAddress
   static hasMany = [memberships: Membership]
}

class Membership {
   String name
   Integer memberPlanId
   static belongsTo = [user: User]
}

//MemberPlan data have 2 types of plan existed in DB
class MemberPlan {
   String type
   Float amount
}

I want the 'memberPlanId' of Membership to reference from MemberPlan. How do I set from there, it should be under belongsTo? And how does the saving query for this 'memberPlanId' property goes?

new User(username: 'input_username', password: 'input_password', emailAddress: 'input_emailAddress')
  .addToMemberShips(new Membership(name: 'input_name', memberPlanId: ?))
  .save()
1

1 Answers

0
votes

You would want to edit the Membership domain as follows

class Membership {
   String name
   MemberPLan memberPlan                 // this is basically a belongsTo relation
   static belongsTo = [user: User]
}

To save a new user you can use

new User(username: 'input_username', password: 'input_password', emailAddress: 'input_emailAddress')
  .addToMemberShips(new Membership(name: 'input_name', memberPlan: MemberPlan.get(1)))
  .save()

MemberPlan.get(1) will fetch the record from the database with id = 1

Hope that helps