Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding init data via Bootstrap.groovy to application

Tags:

grails

My domain layer looks like that:

@Resource(uri='/product')
class BasicProduct {

    String title
    String description
    Double price
    Date creationDate
    Date changedDate

    static constraints = {
        //everything is by default NotNull
        title(blank: false, unique: true)
        description(blank: false)
        price(blank: false, inList: [5,15,25,50,100])
        creationDate(min: new Date())
    }

}

My Bootstrap.groovy contains that code:

class BootStrap {

    def init = { servletContext ->
         new BasicProduct(title: "Product1", description:"blblblblbalablablalbalbablablablablblabalalbllba", price:5).save()
         new BasicProduct(title: "Product2", description:"blblblblbalablablalbalbablablablablblabalalbllba", price:75).save()
         new BasicProduct(title: "Product3", description:"blblblblbalablablalbalbablablablablblabalalbllba", price:50).save()
         new BasicProduct(title: "Product4", description:"blblblblbalablablalbalbablablablablblabalalbllba", price:25).save()
         new BasicProduct(title: "Product5", description:"blblblblbalablablalbalbablablablablblabalalbllba", price:15).save()

         println "initializing data..."
    }

However when I open the \product url I do not see any data.

Any ideas why?

I appreciate your answer!

like image 756
user2051347 Avatar asked Jan 14 '14 20:01

user2051347


1 Answers

For your dates you can do:

Date dateCreated
Date lastUpdated 

static mapping = {
  autoTimestamp true
}
like image 115
brwngrldev Avatar answered Oct 05 '22 07:10

brwngrldev