Rails sunspot N+! 查询问题

michael_roshen · 2013年08月28日 · 最后由 michael_roshen 回复于 2013年08月28日 · 2008 次阅读
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
Article.search(:include => [:comments]) do

end

#1 楼 @quakewang nice, 就是它,谢谢

需要 登录 后方可回复, 如果你还没有账号请 注册新账号