別のフィクスチャでユーザー フィクスチャの ID を参照する方法はありますか。私はmessages.ymlフィクスチャを持っています:
basic:
body: "Hey Barney, wanna go bowling tonight?"
sender: fred
recipient_ids: [users(:barney)]
type: Message
users.yml
barney:
first_name: Barney
last_name: Rubble
別のフィクスチャでユーザー フィクスチャの ID を参照する方法はありますか。私はmessages.ymlフィクスチャを持っています:
basic:
body: "Hey Barney, wanna go bowling tonight?"
sender: fred
recipient_ids: [users(:barney)]
type: Message
users.yml
barney:
first_name: Barney
last_name: Rubble
はい、ERB 構文を使用します。
recipient_id: <%= User.find_by_first_name('Barney').id %>
http://guides.rubyonrails.org/v5.0/testing.html#the-low-down-on-fixtures
これが、フィクスチャが一般的に使用されなくなった大きな理由です。シードファイルを使用することをお勧めします。おそらくseed_fuで。これにより、Ruby でフィクスチャ/シードを作成し、ほぼ何でも行うことができます。