Grails select不会返回正确的数据 [英] Grails select will not return correct data

查看:87
本文介绍了Grails select不会返回正确的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是这个问题的继续。

我有一个地址类,其中包含基本的街道地址信息。我还有一个 User 类,它具有属性 physicalAddress mailingAddress cargoDestinations cargoSources User 类看起来像这样:

I have an Address class which contains basic street address information. I also have a User class which 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

Address 类看起来像这样:

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

一个href =http://moongrails.blogspot.com/2008/12/grails-ajax-chained-select.html =nofollow noreferrer>这里大部分。在步骤5中,我的模板如下所示:

I followed the tutorial here for the most part. In step 5 my template looks like this:

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

问题是,不是只返回 cargoDestinations ,模板将返回与该用户关联的所有地址。如果我将从=$ {account.user.cargoDestinations}更改为<= c $ c> from =$ {account.user.physicalAddress}或 from =$ {account.user.mailingAddress}我得到预期的结果,所以我知道我的问题与 cargoDestinations 变量被映射。

The problem is that instead of returning only cargoDestinations, the template returns ALL addresses associated with that 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 my problem has something to do with how the cargoDestinations variable is mapped. How can I go about fixing this without changing my class files too much?

推荐答案

我最终在我的地址中添加了几个布尔字段类,这使设计更简单,更容易处理。这样我只需要一个类而不是几个几乎相同的类。现在,地址类中的布尔字段指示地址是物理地址,邮寄地址,货源等,还是以上全部。正如@ataylor指出的那样,这个系统使得地址对象只能与我的 User 类中的一个用户关联,但它似乎不会是问题。最糟糕的情况是,多个用户在现实生活中会有相同的地址,而且我的程序需要为每个用户创建一个单独的地址对象,即使这些地址是相同的。

I ended up adding several boolean fields to my address class, which made the design simpler and much easier to work with. This way I only need one class instead of several nearly identical classes. The boolean fields in the Address class now indicate if an address is a physical address, mailing address, cargo source, etc., or all of the above. As @ataylor pointed out, this system makes it so an address object can only be associated with one user from my User class, but it does not seem like that will ever be an issue. The worst case is that a multiple users would have the same address in real life, and my program would require the creation of a separate address object for each of those users, even though the addresses in question would be identical.

这篇关于Grails select不会返回正确的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆