Hi
You need to add votes_count as an attribute in your index, and specify a
sort order.
In your model:
define_index do
indexes word
has votes_count
end
In your controller:
def search
@words = ThinkingSphinx::Search.search params[:search], :page =>
params[:page], :per_page => 1, :order => "votes_count DESC"
end
If your word model is the only model indexed, it might be cleaner to do
the controller search directly on that:
def search
@words = Word.search params[:search], :page => params[:page],
:per_page => 1,
:order => "votes_count DESC"
end
James
Leximo wrote:
> I have a words table that keeps track of the total votes for a word.
> However, I can't seem to sort it by votes. This is what I have so far:
>
> # In words controller
>
> def search
> @words = ThinkingSphinx::Search.search params[:search], :page =>
> params[:page], :per_page => 1#, :order => :votes_count
> end
>
> #In words model
>
> define_index do
> indexes word
>
> # set_property :delta => true
>
> # has id, created_at
> end
>
> # In search model
>
> def search
> @words = Word.search params[:search]
> end
>
> # In my search results
>
> <h1>Search words for your query</h1>
> <% for word in @words %>
> <% if word.class.name == "Word" %>
> <%= render :partial => "words/word", :locals => { :word =>
> word } %>
> <% else %>
> <h1>There were no words for your query</h1><br/><br/>
> <% end %>
> <% end %>
> <%= will_paginate @words %>
>
> # and in my Database
>
> create_table "words", :force => true do |t|
> t.string "word"
> t.string "language"
> t.text "definition"
> t.string "example"
> t.datetime "created_at"
> t.datetime "updated_at"
> t.integer "user_id", :limit => 11
> t.integer "votes_count", :limit => 11, :default => 0
> end
>
>
> I'm trying to sort the search results by "votes_count" so that the
> word with the most votes appears first (Descending).
>
> Could some one help me out, I would really appreciate it.
>
> Thanks
>
> >
-- James Healy <jimmy-at-deefa-dot-com> Wed, 15 Oct 2008 12:58:47 +1100
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---