2010-09-22 147 views
0

我有這個「查詢」活動記錄的查詢與包括不包括

@product_collections = ProductCollection. 
     #includes(:products). #does not do anything 
     joins(:products). #this at least gives me the products (in articles table) 
     group("tags.id"). 
     where("articles.category_id = ?", @category.id). 
     where("articles.available = ?", true). 
     order('tags.name asc') 

這將產生以下SQL

SELECT  "tags".* FROM  "tags" 
INNER JOIN "article_taggings" ON "tags"."id" = "article_taggings"."tag_id" 
INNER JOIN "articles" ON "articles"."id" = "article_taggings"."article_id" 
WHERE  ("tags"."type" = 'ProductCollection') 
AND (articles.category_id = 1) 
AND (articles.available = 't') 
GROUP BY tags.id 
ORDER BY tags.name asc 

我怎麼能設法讓產品在一個(或只一秒鐘)去?

型號:

class Article < ActiveRecord::Base 
    has_many :article_taggings 
    has_many :tags, :through => :article_taggings 
end 

class Product < Article 
    belongs_to :category 
    has_many :product_collections, :through => :article_taggings, :source => :tag 
end 

class ArticleTagging < ActiveRecord::Base 
    belongs_to :article 
    belongs_to :tag 
end 

class Tag < ActiveRecord::Base 
    has_many :article_taggings 
    has_many :articles, :through => :article_taggings 
    has_and_belongs_to_many :web_pages 
end 

class ProductCollection < Tag 
    has_many :products, :through => :article_taggings, :source => :article 
end 
+0

你可以發佈此查詢中涉及的模型嗎? – 2010-09-22 20:47:21

+0

他們在那裏,抱歉,延遲,我沒​​有通知您的評論:( – Jan 2010-09-23 00:50:26

回答

1

你需要把包括後加入。這將解決問題。