Note that there are some explanatory texts on larger screens.

plurals
  1. POIdentifying Model Instance by Two Relations
    primarykey
    data
    text
    <p>I'm trying to pick out an instance of a model ("Package") by its relation to two other models to which it belongs ("Cut" and "Animal"). While there are, say many packages with :cut_id 3 and many with :animal_id 4, there should only be one with both, and I want to pick that one out and display its contents in a table. </p> <p>I've tried the following DIY mess, and it's not really working. (cutfind is a method I created that I know works for calling out all of the cuts associated with the given animal.)</p> <pre><code>&lt;% @animal.cutfind.each do |cut| %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= cut.name %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= number_to_currency(cut.price) %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= cut.package_weight %&gt; lb&lt;/td&gt; &lt;% @a = Package.where(:animal_id =&gt; @animal.id) %&gt; &lt;% @pset = @a.where(:cut_id =&gt; cut.id) %&gt; &lt;% @pset.each do |p| %&gt; &lt;td&gt;&lt;%= p.original %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= p.left %&gt;&lt;/td&gt; &lt;% end %&gt; &lt;/tr&gt; &lt;%end%&gt; </code></pre> <p>Any idea how to do this [better]? Thanks.</p> <p>Update: I tried this other DIY mess and am getting the same problem (the cells aren't even being created, which leads me to believe that @pset is empty).</p> <p>This is in my animal model:</p> <pre><code>def packagefind Package.where(:animal_id =&gt; self.id) end </code></pre> <p>And then I changed the above like so: </p> <pre><code>&lt;td&gt;&lt;%= cut.package_weight %&gt; lb&lt;/td&gt; &lt;% @pset = @animal.packagefind.where(:cut_id =&gt; cut.id) %&gt; &lt;% @pset.each do |p| %&gt; &lt;td&gt;&lt;%= p.original %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= p.left %&gt;&lt;/td&gt; &lt;% end %&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload