Find Record based on the associated attributes <p>I am trying to find a record based on two associated attributes. The Record should be selected, if its association contains those two records.</p> <p>So far, I tried following - Which seemed to me a very bad practice and I want to avoid using it.</p> <pre><code>@size = Spree::OptionValue.find(params[:size]) @color = Spree::OptionValue.find(params[:color]) vari = Spree::Variant.all vari.each do |va| if va.option_values.include?(@size &amp;&amp; @color) @variant = va end end </code></pre> <p>So far, I also tried </p> <pre><code>@variant = Spree::Variant.all(:include =&gt; :option_values, :conditions =&gt; ['option_value.id = ?', params[:color]) </code></pre> <p>This seems to be the way to go, but I can't seem to figure out the right way to get the result. The return error I keep on getting is following:</p> <blockquote> <p>ActiveRecord::StatementInvalid: PG::Error: ERROR: missing FROM-clause entry for table "option_values" LINE 1: ..._option_values_variants"."option_value_id" WHERE (option_val...</p> </blockquote> <p>EDIT:</p> <p>I got it working due to the great help given in the accepted answer:</p> <pre><code>Spree::Variant.joins(:option_values).where("spree_option_values.id in (?)", [size, color]) </code></pre>