嗨,伙计们,这是我的代码:
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.
我试过这个:
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
我得到两个结果(简单的素道代码)
<id: 1, item_type: "Collar"><id:2, item_type:"Sleeve">
第二裁缝:
Tailor.find(2).tailor_items
我得到两个结果(简单的素道代码)
<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 )")
我仍然得到0个结果。
Tailor.where("item_id = 1 and item_type = Collar ") returns: Tailor #1
Tailor.where("item_id = 2 and item_type = Sleeve ") returns: Tailor #1
他们却一无所获。
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 run: Win XP Postgresql Rails 3.2.2
PS: 在多形态组合后完成这个完整的唯一缺少的是 XML 。 : P 否则它只是不够有企业性质而已 。
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)