Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GORM Querying multiple collections

I am working with Grails and MongoDB. I have two domain classes User and AddWebsite. A User hasMany websites and each website belongs to a user. The domain classes are as follows:

class AddWebsite{
String website
User user
static belongsTo = [user: User]
static constraints = {
    website url:true
    user nullable:true
}
}

The Other domain class is as follows:

class User {
    String login
    String password
    static hasMany = [
        addWebsites: AddWebsite
    ]
    static mapping = {
        addWebsites cascade:"all-delete-orphan"
      }
    static constraints = {
        }
    }

I need to query the AddWebsite table based on the current logged in user and get the websites of that particular user. Can anyone suggest any approach?

like image 544
clever_bassi Avatar asked Mar 06 '26 14:03

clever_bassi


1 Answers

I used this approach. May not be most efficient but it works .

def showWebsites(){
    def p = User.findByLogin(session["user"].login)
    def websites = AddWebsite.findAllByUser(p['_id'])
    [websitesList: websites] 
}

And in my GSP I have:

<g:select name="websiteSelection" from="${websitesList.website} " />
like image 82
clever_bassi Avatar answered Mar 09 '26 13:03

clever_bassi