【问题标题】:Active record query with includes does not include包含的活动记录查询不包含
【发布时间】:2010-09-22 18:03:46
【问题描述】:

我有这个“查询”

    @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

【问题讨论】:

  • 您能发布此查询中涉及的模型吗?
  • 他们来了,抱歉耽搁了,我没有收到您的评论通知:(

标签: ruby-on-rails activerecord ruby-on-rails-3


【解决方案1】:

您需要将包含放在连接之后。这样就可以解决问题了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-01
    • 2010-12-27
    • 1970-01-01
    • 2013-07-11
    • 2014-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多