n92
n92

Reputation: 7602

How to save Many to Many domain object in grails

I have two domain classes, Course can belongs to one or many training and Track can have one or many courses.

Class Track{
   string name 
   static hasMany= [courses: Course]

}

Class Course{
   String name
   static belongsTo = [tracks: Track]

}

in the course create a page, I have field

<g:select id="tracks" name="tracks.id" from="${com.springpeople.tms.Track.list()}" optionKey="id" value="${courseInstance?.tracks?.id}" class="many-to-one" noSelection="['null': '']"/>

But track is not getting saved in course, Do i need explicitly pass tracks objects. Is there any way to do it.

Upvotes: 2

Views: 885

Answers (1)

sanghavi7
sanghavi7

Reputation: 754

first you need to save track class object then after you can save Course class object.

in your situation when you were trying to save course object you will not get track id thats why it will through exception.

after saving track class object by save method.

after that use object.addToCourse(courseObject).save(); for saving.

Upvotes: 2

Related Questions