Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7967705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:57:56+00:00 2026-06-04T06:57:56+00:00

Hi guys here is my code: class Tailor < ActiveRecord::Base has_many :tailor_items has_many :order_items

  • 0

Hi guys here is my code:

class Tailor < 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 < ActiveRecord::Base
  belongs_to        :tailor
  belongs_to        :item, polymorphic: true
end

class Collar < ActiveRecord::Base
end

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.

I tried this:

tailors = Tailor.joins(:tailor_items).where("(item_id = ? and item_type = ?)",1,"Collar")
if tailors.count > 1
  tailors.where("(item_id = ? and item_type = ?)",2,"Sleeve")
  if tailors.count > 1
     # and so forth.
  end
end

But I never get a row back.
If I say:

Tailor.find(1).tailor_items 

I get two results (sudo code for simplicity)

<id: 1, item_type: "Collar"><id:2, item_type:"Sleeve"> 

and for second tailor:

Tailor.find(2).tailor_items 

I get two results (sudo code for simplicity)

<id: 1, item_type: "Collar"><id:3, item_type:"Sleeve"> 

but when I try to chain them in the query its no worky…
Not even if I put it all in one where:

Tailor.where("(item_id = 1 and item_type = 'Collar') and (item_id = 2 and item_type = 'Sleeve')")

I still get 0 results.

Tailor.where("item_id = 1 and item_type = 'Collar'") returns: Tailor #1
Tailor.where("item_id = 2 and item_type = 'Sleeve'") returns: Tailor #1

but together they return nothing.

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'))

I am confused..

Thanks for your help.

I run:
Win XP
Postgresql
Rails 3.2.2

PS: The only thing missing to make this complete after a polymorphic join is a bit of XML. 😛 Otherwise its just not enterprise-y enough..

EDIT:
Implementing Rob di Marcos scope, I get this SQL:

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'))

This returns
2 tailors instead of only tailor 1 who can do both (while tailor 2 cant do sleeve #2)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T06:57:57+00:00Added an answer on June 4, 2026 at 6:57 am

    The problem is that the where needs to match on two rows. I generally will use sub-queries to test for this. So something like

    Tailor.where("exists (select 'x' from tailor_items where 
             tailor_id = tailors.id and tailor_items.item_id = ? and 
             tailor_items.item_type=?)", 1, 'Collar').
           where("exists (select 'x' from tailor_items where 
             tailor_id = tailors.id and tailor_items.item_id = ? and 
             tailor_items.item_type=?)", 2, 'Sleeve')
    

    In this example, I have one sub-query for each tailor item I am looking for. I could easily make this a scope on Tailor like:

    class Tailor
      # ....
      scope :with_item, lambda{ |item_id, item_type | 
          where("exists (select 'x' from tailor_items where 
         tailor_id = tailors.id and tailor_items.item_id = ? and 
         tailor_items.item_type=?)", item_id, item_type)
         }
    

    and then be able to chain my Tailor request

    Tailor.with_item(1, 'Collar').with_item(2, 'Sleeve')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey Guys, here is my code for this, the only help i get from
Python and django newbie question, here is code: class Client(User) #some fields client=Client() client.save()
guys. I use XMLSerializer to keep and restore program options. Here is the code:
Here is my code guys, what am I doing wrong (I am forced to
Hey guys, I have this code within a function inside a class that is
Good morning/afternoon/evening guys Here is the thing. I'm making a registering gsp that it
guys. Here's another sample script in VBScript. It opens internet explorer, navigates to google,
Hey guys I have a little issue here. I have a panel where I
guys! I've noticed that here are a lot of people who are familiar with
Hey guys got a quick question here, how do i set up TweetStream after

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.