Rails sunspot N+! 查询问题

michael_roshen · 发布于 2013年08月28日 · 最后由 michael_roshen 回复于 2013年08月28日 · 1073 次阅读
7614
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
共收到 2 条回复
162
Article.search(:include => [:comments]) do

end
7614

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

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