0

I would like to create around 30 domain objects inside a controller. Here is how I wrote for the first object and it works fine (can see the output on index.gsp). Now, if I have to do the same for the 30 objects I need, should I have 30 different names or is there a simpler way?

class VendorController {

    def index() {
        def myvendor = new Vendor(name: "myVendor")

        [vendor: vendor]
    }

I know I can create objects in the BootStrap.groovy, but that isn't working (check Grails error: table or view does not exist) Until I figure out the mistake there, I want to create the objects in the controllers.

Community
  • 1
  • 1
NanoNi
  • 315
  • 4
  • 16

1 Answers1

1

I'd do this in a service with a transaction, but that aside, you can do the following syntax:

(1..10).each { idx ->
  new Vendor(name: "myVendor_${idx}").save()
}
Gregg
  • 34,973
  • 19
  • 109
  • 214