1

Fixture プラグインを使用して Grails アプリケーションにデータをロードしようとしています。

class Author {
  String name 
}

class Book {
  String title
  hasMany = [authors:Author]
}

Author を別のファイルにロードし、それを Book に含めます。

//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}"]

これまでに試したことのほとんどすべてで、一致するエディターまたは変換戦略が見つからないというエラーが発生しました。

事前に感謝します Grails 達人!

4

2 に答える 2

1

以下の回答(およびこの回答に対する以前の編集)に基づいて、これはおそらくより良い方法です:

def dynamicallyBuiltAuthorList = [ 'author1', 'author2', 'author100' ].collect {
  ref( "$it" )
}
authors = dynamicallyBuiltAuthorList
于 2012-09-12T10:14:17.757 に答える
0

結局のところ、答えは単純です。

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

助けてくれたティムに感謝します!

于 2012-09-12T22:09:51.707 に答える