Grails - using a multiple list belongs, but only one at a time

If I want to use a domain class, for example. MoneyTransaction for two completely different purposes, that is:

1) when the customer places an order

2) when the participant receives payment

so i have something like:

class Order { static hasMany = [transactions: MoneyTransaction] } class Member { static hasMany = [payments: MoneyTransaction] } 

and

 class MoneyTransaction { static belongsTo = [order: Order, member: Member] static constraints = { order(nullable: true) member(nullable: true) } } 

and then, essentially, only use one name belongs / association at a time, is this a pretty "standard" use, or do I need to switch this simulation? Currently MoneyTransaction has both credit cards and ACH payment options, as they are used for orders. Only part of the ACH will be used for payments.

+4
source share
1 answer

The domain class definitions that you posted look right based on your requirements. One of the modifications that I would make here would be to add a custom validator to make sure that both orders and elements are not equal to zero at the same time.

  static constraints = { order(nullable: true, validator: {field, inst -> inst.member || field}) member(nullable: true) } 
+6
source

Source: https://habr.com/ru/post/1388058/


All Articles