2010-09-22 148 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

你需要把包括后加入。这将解决问题。