Note that there are some explanatory texts on larger screens.

plurals
  1. PORails Find based on a set existing in through a relation
    primarykey
    data
    text
    <p>I am trying to select an instance based on a relation of that instance containing a set. A simplified example follows:</p> <pre><code>class Product::Variation &lt; ActiveRecord::Base attr_accessible :name, :product_id, :quantity belongs_to :product has_many :bids, :foreign_key =&gt; :product_variation_id has_many :product_variation_property_values, :class_name =&gt; 'Product::Variation::PropertyValue' has_many :property_values, :through =&gt; :product_variation_property_values, :class_name =&gt; 'Property::Value' end class Product::Variation::PropertyValue &lt; ActiveRecord::Base attr_accessible :property_value_id, :variation_id, :property_id belongs_to :variation belongs_to :property_value, :class_name =&gt; 'Property::Value' end class Property &lt; ActiveRecord::Base attr_accessible :name has_many :values, :class_name =&gt; 'Property::Value' end class Property::Value &lt; ActiveRecord::Base attr_accessible :content belongs_to :property belongs_to :partner end </code></pre> <p>So now I want to do something like the following (in psuedo code):</p> <pre><code>Variation.where(:property_values includes [Property::Value.find(1), Property::Value.find(2)]) </code></pre> <p>Is there a way to do this using ActiveRecord?</p> <p>Thanks and let me know if you need more info.</p> <p><em><strong>More Info</em></strong></p> <p>I tried the following:</p> <pre><code>Product::Variation.joins(:property_values).where('property_values.id' =&gt; [Property::Value.find(1).id, Property::Value.find(2).id]).first </code></pre> <p>...which is the following SQL...</p> <pre><code>SELECT "product_variations".* FROM "product_variations" INNER JOIN "product_variation_property_values" ON "product_variation_property_values"."variation_id" = "product_variations"."id" INNER JOIN "property_values" ON "property_values"."id" = "product_variation_property_values"."property_value_id" WHERE "property_values"."id" IN (1, 2) </code></pre> <p>...and this returns...</p> <pre><code>#&lt;Product::Variation id: 25, product_id: 1, quantity: 39, created_at: "2013-11-18 00:18:45", updated_at: "2013-11-18 00:18:45"&gt; </code></pre> <p>But if I do:</p> <pre><code>Product::Variation.find(25).property_values.inspect </code></pre> <p>...I get...</p> <pre><code>[#&lt;Property::Value id: 1, property_id: 1, content: "XS", created_at: "2013-11-18 00:18:45", updated_at: "2013-11-18 00:18:45", color: nil, color_texture: nil, secondary_color: nil&gt;, #&lt;Property::Value id: 6, property_id: 2, content: "Dark Wood", created_at: "2013-11-18 00:18:45", updated_at: "2013-11-18 00:18:45", color: "#855E42", color_texture: "striped", secondary_color: "#FFB90F"&gt;] </code></pre> <p>But I'm looking for the <code>Product::Variation</code> that contains both <code>Property::Value</code> 1 and 2. This is returning those that contain 1 or 2.</p>
    singulars
    1. This table or related slice is empty.
    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.
    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