Schoolboy error causing problems with has_many?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Schoolboy error causing problems with has_many?

James Byatt
Hello there,

I'm trying to get started in a new grails (1.0.4) project and I'm having a bit of trouble getting hasMany to work.

I've defined two domain classes, Gallery and Image, as follows:


class Gallery {

    static constraints = {}
    static hasMany = { images: Image }

        String name
        String location
        String description
        Date takenOn
       
}


class Image {
        static constraints = { showIndex(nullable: true) }
        String fileName
        String caption
        short showIndex
}

I'm putting together the index view for gallery - it will eventually show gallery name, description, etc and provide a scrollable area for the gallery images...but I'm getting a runtime exception whenever I attempt to access the images of a gallery. Here's what I get in the error:

Exception Message: No such property: images for class: Gallery
Caused by: No such property: images for class: Gallery

Here's a snipped version of what's in the .gsp:

<g:each var="gallery" in="${galleries}">
        <g:each var="image" in="${gallery.images}">
                    ...
        </g:each>
</g:each>

and the index method of the GalleryController:
def index = {
    [galleries: Gallery.list([fetch:[images:"join"]])]  
    }

Now, if I add a
    Set<Image> images
member to the Gallery domain, everything works fine, but I was under the impression that this
a.) wasn't required
b.) might cause problems persisting the relationship to the DB, in fact if I examine the database objects grails has created in the database there does not seem to be any structure to persist this anyway (there are only two tables Gallery and Image - no link table) - is this the problem? Should I need to create the link table or FK relationship myself?

This is probably a complete newbie error, but I'm somewhat stumped at this point so I'd really appreciate any suggestions ( including RTFM with a suitable M link ;-) ).

Thanks in advance,
James.
       
Reply | Threaded
Open this post in threaded view
|

Re: Schoolboy error causing problems with has_many?

Dierk König
>    static hasMany = { images: Image }

make this

static hasMany = [ images: Image ]

cheers
Dierk

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Schoolboy error causing problems with has_many?

Robert Fischer
In reply to this post by James Byatt
You want [ images: Image ], not { images: Image }.

Constraints look fine.

~~ Robert.

James Byatt wrote:

> Hello there,
>
> I'm trying to get started in a new grails (1.0.4) project and I'm having a
> bit of trouble getting hasMany to work.
>
> I've defined two domain classes, Gallery and Image, as follows:
>
>
> class Gallery {
>
>     static constraints = {}
>     static hasMany = { images: Image }
>
> String name
> String location
> String description
> Date takenOn
>
> }
>
>
> class Image {
> static constraints = { showIndex(nullable: true) }
> String fileName
> String caption
> short showIndex
> }
>
> I'm putting together the index view for gallery - it will eventually show
> gallery name, description, etc and provide a scrollable area for the gallery
> images...but I'm getting a runtime exception whenever I attempt to access
> the images of a gallery. Here's what I get in the error:
>
> Exception Message: No such property: images for class: Gallery
> Caused by: No such property: images for class: Gallery
>
> Here's a snipped version of what's in the .gsp:
>
> <g:each var="gallery" in="${galleries}">
> <g:each var="image" in="${gallery.images}">
>                     ...
> </g:each>
> </g:each>
>
> and the index method of the GalleryController:
> def index = {
>     [galleries: Gallery.list([fetch:[images:"join"]])]  
>     }
>
> Now, if I add a
>     Set<Image> images
> member to the Gallery domain, everything works fine, but I was under the
> impression that this
> a.) wasn't required
> b.) might cause problems persisting the relationship to the DB, in fact if I
> examine the database objects grails has created in the database there does
> not seem to be any structure to persist this anyway (there are only two
> tables Gallery and Image - no link table) - is this the problem? Should I
> need to create the link table or FK relationship myself?
>
> This is probably a complete newbie error, but I'm somewhat stumped at this
> point so I'd really appreciate any suggestions ( including RTFM with a
> suitable M link ;-) ).
>
> Thanks in advance,
> James.
>

--
~~ Robert Fischer.
Grails Training        http://GroovyMag.com/training
Smokejumper Consulting http://SmokejumperIT.com
Enfranchised Mind Blog http://EnfranchisedMind.com/blog

Check out my book, "Grails Persistence with GORM and GSQL"!
http://www.smokejumperit.com/redirect.html

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Schoolboy error causing problems with has_many?

burtbeckwith
In reply to this post by James Byatt
That should be:

   static hasMany = [images: Image]

since it's a Map, not a Closure.

M link: http://grails.org/doc/1.0.x/

Burt

>
> Hello there,
>
> I'm trying to get started in a new grails (1.0.4) project and I'm having a
> bit of trouble getting hasMany to work.
>
> I've defined two domain classes, Gallery and Image, as follows:
>
>
> class Gallery {
>
>     static constraints = {}
>     static hasMany = { images: Image }
>
> String name
> String location
> String description
> Date takenOn
>
> }
>
>
> class Image {
> static constraints = { showIndex(nullable: true) }
> String fileName
> String caption
> short showIndex
> }
>
> I'm putting together the index view for gallery - it will eventually show
> gallery name, description, etc and provide a scrollable area for the gallery
> images...but I'm getting a runtime exception whenever I attempt to access
> the images of a gallery. Here's what I get in the error:
>
> Exception Message: No such property: images for class: Gallery
> Caused by: No such property: images for class: Gallery
>
> Here's a snipped version of what's in the .gsp:
>
> <g:each var="gallery" in="${galleries}">
> <g:each var="image" in="${gallery.images}">
>                     ...
> </g:each>
> </g:each>
>
> and the index method of the GalleryController:
> def index = {
>     [galleries: Gallery.list([fetch:[images:"join"]])]  
>     }
>
> Now, if I add a
>     Set<Image> images
> member to the Gallery domain, everything works fine, but I was under the
> impression that this
> a.) wasn't required
> b.) might cause problems persisting the relationship to the DB, in fact if I
> examine the database objects grails has created in the database there does
> not seem to be any structure to persist this anyway (there are only two
> tables Gallery and Image - no link table) - is this the problem? Should I
> need to create the link table or FK relationship myself?
>
> This is probably a complete newbie error, but I'm somewhat stumped at this
> point so I'd really appreciate any suggestions ( including RTFM with a
> suitable M link ;-) ).
>
> Thanks in advance,
> James.
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email