使用Grails Fixture插件动态加载子项

使用Grails Fixture插件动态加载子项,grails,groovy,Grails,Groovy,我正在尝试使用Fixture插件在Grails应用程序中加载数据 class Author { String name } class Book { String title hasMany = [authors:Author] } 我将作者加载到一个单独的文件中,并将其包含在第一本书中 //author fixture fixture { author1(Author) { name = 'Ken Follett' } author2(Author) {

我正在尝试使用Fixture插件在Grails应用程序中加载数据

class Author {
  String name 
}

class Book {
  String title
  hasMany = [authors:Author]
}
我将作者加载到一个单独的文件中,并将其包含在第一本书中

//author fixture
fixture {
  author1(Author) {
    name = 'Ken Follett'
  }
  author2(Author) {
    name = 'Martin Fowler'
  }
}

//book fixture
fixture {
  include 'author'
  "book"(Book) {
    title = 'Your favorite book'
    authors = [author1, author2]
  }
}
这一切都很好。我不能做的是替换[author1,author2],这样我就可以动态(随机)分配作者。比如:

def dynamicallyBuiltAuthorList = "author1, author2, author100"
authors = ["${dynamicallyBuiltAuthorList}"]
到目前为止,我尝试的几乎所有方法都给了我一个“未找到匹配编辑器或转换策略”错误


提前感谢圣杯大师

根据下面的答案(以及之前对该答案的编辑),这可能是一种更好的方法:

def dynamicallyBuiltAuthorList = [ 'author1', 'author2', 'author100' ].collect {
  ref( "$it" )
}
authors = dynamicallyBuiltAuthorList

根据下面的答案(以及之前对此答案的编辑),这可能是一种更好的方法:

def dynamicallyBuiltAuthorList = [ 'author1', 'author2', 'author100' ].collect {
  ref( "$it" )
}
authors = dynamicallyBuiltAuthorList

最后,答案很简单:

def authorList = []
def dynamicallyBuiltAuthorList = [ 'author1', 'author2', 'author100' ].collect {
   authorList.add(ref("$it"))
}
authors = authorList

谢谢蒂姆的帮助

最后,答案很简单:

def authorList = []
def dynamicallyBuiltAuthorList = [ 'author1', 'author2', 'author100' ].collect {
   authorList.add(ref("$it"))
}
authors = authorList

谢谢蒂姆的帮助

谢谢Tim,great insight,最终的确切答案是相似的,只需将
这个“$it”
替换为
另一个列表。添加(ref($it”)
,就可以了。谢谢Tim,great insight,最终的确切答案是相似的,只需将
这个“$it”
替换为
另一个列表。添加(ref($it”))
这就解决了。根据这一点更新了我的答案。如果您正在进行
收集
,则不需要添加辅助列表:-)根据此更新了我的答案。如果您正在执行收集操作,则不需要将辅助列表添加到:-)