2011-12-16 67 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