Hi, maybe I need something exclusive, but I think this is common
functionality, and I'am on wrong way.

Ok, here example:

I have Post model like this:

class Post < ActiveRecord::Base
  has_many :comments

  define_index do
    indexes title
    indexes body

    has comments(:id), :as => :comments_ids
    has comments(:created_at), :as => :comments_created_at, :type
=> :datetime
  end
end

Now I need to show most popular comments in last hour, with number
comments for each comment for this hour.
Yes, I can find Posts like this:

Post.search(:with => { :comments_created_at => 1.hour.ago..Time.now})
But this don't count matched comments, so i need to make many SQL's to
count this.

Maybe I miss some thing and sphinx / Thinking Sphinx, can do this in
more elegant way?

P.S.

My another option, is to search comments (not posts), and group them
by post_id
Like this:

  def self.search_for_popular_and_count_comments(time_range)
    posts = []
    tmp = {}

    posts_ids = Comment.search_for_ids(:with => {:created_at =>
time_range}, :group => 'post_id')

    posts_ids.each_with_groupby_and_count do |group, count|
      tmp[group] = count
    end

    posts = Post.find_all_by_id tmp.keys

    posts.each do |post|
      post.searched_comments_count = tmp[posts.id]
    end
    posts
  end

But, i think must be another simple way to do this.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Thinking Sphinx" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/thinking-sphinx?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to