Rails sunspot N+! 查询问题

michael_roshen · August 28, 2013 · Last by michael_roshen replied at August 28, 2013 · 2002 hits
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, 就是它,谢谢

You need to Sign in before reply, if you don't have an account, please Sign up first.