That's because params.districtId is String and you are assigning it to the property of type District.
So you have to retrieve an instance of District first, with id you can use this:
thana.district = District.get(params.districtId?.toLong())
Domanda
I have a domain named District where I will input only district name and it works fine. I have another domain named Thana where I need to have District id as foreign key cause there is so many thana under a district. And I want to save the thana name and the district ID as foreign key value in thana table. That's why I am getting the district ID from a combo addThana view. But when I am assigning the value its giving error. Can anyone please help me on this please ? Here are my process below :
my district domain >>
class District {
String districtName
static constraints = {
}
static mapping = {
table('district')
version(false)
}
}
my thana domain >>
class Thana {
String thanaName
District district
static constraints = {
}
static mapping = {
table('thana')
version(false)
district column: 'district_id'
}
}
my save thana method >>
def saveThana(){
println(params)
Thana thana = new Thana()
thana.district = params.districtId
thana.thanaName = params.thanaName
thana.save()
}
and the error message >>
Message : argument type mismatch
Soluzione
That's because params.districtId is String and you are assigning it to the property of type District.
So you have to retrieve an instance of District first, with id you can use this:
thana.district = District.get(params.districtId?.toLong())
Altri suggerimenti
The reason is Thana Class want a District Object Not a district_id to save District as foregin key relation to Thana Table.
What you should do more clear way .
def saveThana(){
println(params)
try {
Thana thana = new Thana()
thana.district = District.findById(params?.districtId) //check nulls okay?
thana.thanaName = params?.thanaName.toString()
thana.save(failonSave:true)
}
catch(exception ex){
println ("Save Error":ex.getMessage());
}
}