开发者

Active record query with includes does not include

开发者 https://www.devze.com 2023-01-17 08:43 出处:网络
I have got this \'query\' @product_collections = ProductCollection. #includes(:products). #does not do anything

I have got this 'query'

    @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')

this produces the following sql

SELECT     "tags".* FROM       "tags"  
INNER JOIN "a开发者_运维知识库rticle_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

how could I manage to get the products in one (or only one second) go?

Models:

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


You need to put the includes after joins. That will solve the problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消