Access Attribute of Rails Test Fixture in Associated FixtureSet
<h1>Background</h1>
<p>My application defines a one-to-many relationship between Employee and Company models. I've assigned the Employee fixture a company using the label of the Company fixture (<code>37_signals</code>).  However, I also need to assign the <code>company_uuid</code>, which is generated by <code>SecureRandom.uuid</code>.</p>
<br>
<h1>Example</h1>
<h2>app/models/</h2>
<h3>employee.rb</h3>
<pre><code>class Employee
  belongs_to :company
end
</code></pre>
<h3>company.rb</h3>
<pre><code>class Company
  has_many :employees
end
</code></pre>
<br>
<h2>test/fixtures/</h2>
<h3>employees.yml</h3>
<pre><code>employee:
  name: dhh
  company: 37_signals 
  company_uuid: &lt;%= &quot;Access the 37_signals company fixture's uuid here!&quot; %&gt;
</code></pre>
<h3>companies.yml</h3>
<pre><code>37_signals:
  name: $LABEL
  company_uuid: &lt;%= SecureRandom.uuid %&gt;
</code></pre>
<br>
<h1>Question</h1>
<p>How can I access the attribute of a Fixture in another FixtureSet?</p>
<br>
<h1>Attempted</h1>
<p>I've attempted to use the following lines as solutions:</p>
<pre><code>company_uuid: &lt;%= ActiveRecord::FixtureSet.identify(:37_signals).company_uuid %&gt;
</code></pre>
<p>The above finds the company's primary key <code>id</code> value, then calls the <code>company_uuid</code> method, which is undefined for the integer. This is invalid.</p>
<pre><code>company_uuid: &lt;%= companies(:37_signals).company_uuid %&gt;
</code></pre>
<p>The above finds reports <code>undefined method 'companies' for main:Object</code></p>
<p><strong>Is there a conventional way to solve this problem?</strong></p>