The following test works in a Grails controller:
Domain class:
package freight
class CodeList {
private static final long serialVersionUID = 1
String codeType
String code
String description
static constraints = {
codeType nullable: false, blank: false
code nullable: false, blank: false, unique: 'codeType'
description nullable: true, blank: true
}
static mapping = {
}
}
Code in controller action:
def o = CodeList.get(10254)
println o as JSON
Output:
{"id":10254,"code":"AFR","codeType":"AOC_CODE","description":"Africa"}
When I try this in grails console
I get this error:
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'freight.CodeList : 10254' with class 'freight.CodeList' to class 'freight.CodeList'
at freight.CodeList.get(CodeList.groovy)
at freight.CodeList$get.call(Unknown Source)
at ConsoleScript1.run(ConsoleScript1:3)
What's wrong with this?
The following runs without error and gets the same row:
def row = CodeList.findById(10254)
def rows = CodeList.findAllWhere(id: 10254L) // println rows[0]
Alternative solution
I installed the Grails web-based Groovy console plugin and that works much better and faster than grails console
. Wish I have found this sooner.