Choosing Grails Will Not Return Correct Data

This is a continuation of this issue.

I have an Address class that contains basic street address information. I also have a User class that has the attributes physicalAddress , mailingAddress , cargoDestinations and cargoSources . The User class looks something like this:

 class User { String username String password String firstName String lastName String businessName String phoneNumber Address physicalAddress Address mailingAddress static hasMany = [accounts:Account, cargoSources:Address, cargoDestinations:Address, cargoes:Cargo, loadsLogged:Load, loadsDelivered:Load] Set accounts, cargoSources, cargoDestinations, cargoes static mappedBy = [loadsLogged:"loggedBy", loadsDelivered:"deliveredBy"] //some other stuff after this 

And the Address class looks something like this:

  class Address { static belongsTo = [user:User] String streetAddress String city String state String zip BigDecimal taxRate //some other stuff after this 

I mostly followed the tutorial here . In step 5, my template looks like this:

 <g:select from="${account.user.cargoDestinations}" name="cargoDestinations" value=""> </g:select> 

The problem is that instead of returning only cargoDestinations template returns ALL addresses associated with this user. If I change from="${account.user.cargoDestinations}" to from="${account.user.physicalAddress}" or from="${account.user.mailingAddress}" , I get the expected result, so I know that my problem is with the way the cargoDestinations variable is cargoDestinations . How can I fix this without changing the class files too much?

+4
source share
2 answers

As a result, I added several Boolean fields to my address class, which simplified the work and simplified the work. Thus, I need only one class instead of several almost identical classes. Boolean fields in the Address class now indicate whether the address is a physical address, postal address, source of cargo, etc., Or all of the above. As @ataylor noted, this system makes it possible for an address object to be associated with only one user from my User class, but it seems like this will never be a problem. The worst case is that several users will have the same address in real life, and my program will require the creation of a separate address object for each of these users, even if the corresponding addresses are identical.

0
source

As you configured your addresses, they all refer to the user in the user_id column. You will need to add several fields to the address in order to distinguish how they are related to the user, similar to how you have mapped “Loads”. For instance:

 class Address { static belongsTo = [cargoSourceFor: User, cargoDestinationFor: User] ... } class User { ... static hasMany = [cargoSources:Address, cargoDestinations:Address] static mappedBy = [cargoSources: "cargoSourceFor", cargoDestinations: "cargoDestinationFor"] ... } 

If you are familiar with SQL, then when setting up mappings it is useful to use grails schema-export and watch target/ddl.sql .

+1
source

All Articles