English 中文(简体)
铁路:有多种条件结合(多变)的Postgresql
原标题:Rails: Postgresql where with multiple conditions with join (polymorphic)

嗨,伙计们,这是我的代码:

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)

最佳回答

问题是两个行需要匹配的位置。 我通常会使用子查询来测试。 类似的东西

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 )

在这个例子中,我为每个我要找的裁缝物品 都有一个小小房间。我可以很容易地把这个范围 做成一个关于裁缝的东西,比如:

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

然后能够将我的定制请求链链起来

Tailor.with_item(1,  Collar ).with_item(2,  Sleeve )
问题回答

暂无回答




相关问题
摘录数据

我如何将Excel板的数据输入我的Django应用? I m将PosgreSQL数据库作为数据库。

Postgres dump of only parts of tables for a dev snapshot

On production our database is a few hundred gigabytes in size. For development and testing, we need to create snapshots of this database that are functionally equivalent, but which are only 10 or 20 ...

How to join attributes in sql select statement?

I want to join few attributes in select statement as one for example select id, (name + + surname + + age) as info from users this doesn t work, how to do it? I m using postgreSQL.

What text encoding to use?

I need to setup my PostgreSQL DB s text encoding to handle non-American English characters that you d find showing up in languages such as German, Spanish, and French. What character encoding should ...

SQL LIKE condition to check for integer?

I am using a set of SQL LIKE conditions to go through the alphabet and list all items beginning with the appropriate letter, e.g. to get all books where the title starts with the letter "A": SELECT * ...

热门标签