def all
@articles = Article.all
end
Article Load (0.1ms) SELECT "articles".* FROM "articles" (0.1ms) SELECT COUNT() FROM "comments" WHERE "comments"."article_id" = 1 (0.1ms) SELECT COUNT() FROM "comments" WHERE "comments"."article_id" = 2 (0.1ms) SELECT COUNT(*) FROM "comments" WHERE "comments"."article_id" = 3
解决办法:
1.
def all
@articles = Article.includes(:comments).all
end
2.
def all
@articles = Article.find(:all, :include => :comments)
end
Article Load (0.2ms) SELECT "articles".* FROM "articles" Comment Load (0.2ms) SELECT "comments".* FROM "comments" WHERE "comments"."article_id" IN (1, 2, 3)
但是如果用 sunspot 后,查询结果和 Article.all 一样,应该怎么解决呢?
def all
@search = Article.search do
fulltext params[:search]
with(:published_at).less_than(Time.zone.now)
facet(:publish_month)
with(:publish_month, params[:month]) if params[:month].present?
end
@articles = @search.results
end