Note that there are some explanatory texts on larger screens.

plurals
  1. PORails: Postgresql where with multiple conditions with join (polymorphic)
    primarykey
    data
    text
    <p>Hi guys here is my code:</p> <pre><code>class Tailor &lt; ActiveRecord::Base has_many :tailor_items has_many :order_items [:collars, :sexes, :sleeves].each do |attribute| has_many attribute, through: :tailor_items, source: :item, source_type: attribute.to_s.classify end end class TailorItem &lt; ActiveRecord::Base belongs_to :tailor belongs_to :item, polymorphic: true end class Collar &lt; ActiveRecord::Base end </code></pre> <p>What I need to do is this: For a given shirt I need to select a tailor. A shirt can have a collar, male/female or a certain type of sleeve. Some tailors can make all collars but only a few sleeves, others can make only male stuff, etc. The priority doesnt matter for this example. The idea is that I end up with 1 tailor.</p> <p>I tried this:</p> <pre><code>tailors = Tailor.joins(:tailor_items).where("(item_id = ? and item_type = ?)",1,"Collar") if tailors.count &gt; 1 tailors.where("(item_id = ? and item_type = ?)",2,"Sleeve") if tailors.count &gt; 1 # and so forth. end end </code></pre> <p>But I never get a row back. If I say:</p> <pre><code>Tailor.find(1).tailor_items </code></pre> <p>I get two results (sudo code for simplicity)</p> <pre><code>&lt;id: 1, item_type: "Collar"&gt;&lt;id:2, item_type:"Sleeve"&gt; </code></pre> <p>and for second tailor:</p> <pre><code>Tailor.find(2).tailor_items </code></pre> <p>I get two results (sudo code for simplicity)</p> <pre><code>&lt;id: 1, item_type: "Collar"&gt;&lt;id:3, item_type:"Sleeve"&gt; </code></pre> <p>but when I try to chain them in the query its no worky... Not even if I put it all in one where:</p> <pre><code>Tailor.where("(item_id = 1 and item_type = 'Collar') and (item_id = 2 and item_type = 'Sleeve')") </code></pre> <p>I still get 0 results.</p> <pre><code>Tailor.where("item_id = 1 and item_type = 'Collar'") returns: Tailor #1 Tailor.where("item_id = 2 and item_type = 'Sleeve'") returns: Tailor #1 </code></pre> <p>but together they return nothing.</p> <pre><code>Tailor Load (0.0ms) SELECT "tailors".* FROM "tailors" INNER JOIN "tailor_items" ON "tailor_items"."tailor_id" = "tailors"."id" WHERE ((tailo r_items.item_id = 1 and tailor_items.item_type = 'Collar') and (tailor_items.ite m_id = 2 and tailor_items.item_type = 'Sleeve')) </code></pre> <p>I am confused..</p> <p>Thanks for your help.</p> <p>I run: Win XP Postgresql Rails 3.2.2</p> <p>PS: The only thing missing to make this complete after a polymorphic join is a bit of XML. :P Otherwise its just not enterprise-y enough..</p> <p>EDIT: Implementing Rob di Marcos scope, I get this SQL:</p> <pre><code>SELECT "tailors".* FROM "tailors" WHERE (EXISTS(SELECT * FROM tailor_items WHERE tailor_items.item_id = 1 and tailor_items.item_type = 'Collar')) AND (exists(select * from tailor_items where tailor_items.item_id = 2 and tailor_items.item_type = 'Sleeve')) </code></pre> <p>This returns 2 tailors instead of only tailor 1 who can do both (while tailor 2 cant do sleeve #2)</p>
    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.
    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