在Grails中使用多对一

时间:2013-03-18 16:08:09

标签: hibernate grails gorm

我已经创建了多对一的N:1关系

class Score {
    Double value
    Criteria criteria
    static belongTo = [criterias: Criteria]
}

class Criteria {
    String name
}

所以我的问题是我想使用g:select将某些标准名称的分数存储到数据库中。 我有这个表格

   <g:form controller="audition" action="assignScore">

    <label for="vocal" class="control-label">vocal</label>
    <div class="controls">
        <g:select  from="${['0','10','20','30','40','50','60','70','80','90','100']}" name="vocal" value="score"   noSelection="['':'-choose Score please-']"/>
        <span class="help-inline">${hasErrors(bean: contestantInstance, field: 'vocal', 'error')}</span>

</div>

    <label for="stage" class="control-label">stage</label>
    <div class="controls">
        <g:select  from="${['0','10','20','30','40','50','60','70','80','90','100']}" name="stage" value="score"   noSelection="['':'-choose Score please-']"/>
        <span class="help-inline">${hasErrors(bean: contestantInstance, field: 'stage', 'error')}</span>

</div>
       <div class="form-actions">
           <g:submitButton name="save" class="btn btn-primary" value="save" />
           <button class="btn" type="reset">Cancel</button>
       </div>

   </g:form>

和控制器中的操作

 def assignScore = {
    //find a criteria  name
  def criteriaInstance =  Criteria.findByNameLike(params.vocal)  ?: new Criteria(name: "vocal").save(flush: true)
    if (params.vocal == "vocal"){
    def score = new Score()

        score.properties=params.vocal


        criteriaInstance.addToScores(score)
        criteriaInstance.save(flush: true)
    }

    def criteriaInstanceStage =  Criteria.findByNameLike(params.stage)  ?: new Criteria(name: "stage").save(flush: true)

    if (params.stage == "stage"){
        def score = new Score()
        score.properties=params.stage

        criteriaInstanceStage.addToScores(score)
        criteriaInstanceStage.save(flush: true)
    }


}

问题是我无法保存与某些标准相关的分数

1 个答案:

答案 0 :(得分:1)

请参阅documentation您可以使用g:选择没有任何问题。

<g:select name="criteria"
          from="${Criteria.list()}"
          value="${criteria}"
 />

请注意,标准可能是保留字。