2011-12-16 66 views
0

命名范围,我有三个型号:产品,展示位置,类别写作轨道

我试图写一个名字的范围,只有选择产品不特定集合。

products has_many :collections, :through => :placements

​​

我得到了这么远:

scope :not_in_front, joins(:collections).where('collections.id IS NOT ?', 4) 

但是产生什么,我期待在查询相反:

Product Load (0.3ms) SELECT "products".* FROM "products" INNER JOIN "placements" ON "products"."id" = "placements"."product_id" WHERE "placements"."collection_id" = 4 

任何想法如何写这个只选择不是我的产品那个特别的收藏?

回答

0

命名的范围是越来越太丑陋了,所以我这个去了。不知道这是最好的方式,但它,它的工作原理...

def self.not_on_top_shelf 
    top_shelf = Collection.find_by_handle('top-shelf') 
    products = Product.find(:all, :order => "factor_score DESC") 
    not_on_top_shelf = products.map {|p| p unless p.collections.include?(top_shelf)} 
    not_on_top_shelf.compact #some products may not be in a collection 
    end 
0

而不是collections.id IS NOT 4尝试collections.id != 4

+0

我已经试过了...得到相同的查询 – Slick23 2011-12-16 04:15:56