Hi, guys:
Strange search result with conditions in find_by_contents!
first of all, i’ve installed the acts_as_ferret to my project vender
folder by ‘ruby script/plugin install
svn://projects.jkraemer.net/acts_as_ferret/tags/stable/acts_as_ferret’
in my SearchController
def searchforum
if !params[:doSearch].nil?
if params[:searchTerms].nil? || params[:searchTerms] == “”
flash[:notice] = ‘Please enter some words to search on.’
else
@conditions = " 1 = 1";
if !params[:dateRange].nil? && params[:dateRange] != “”
@conditions += " and creationDate >= " + params[:dateRange]
end
if !params[:forumID].nil? && params[:forumID] != “”
@conditions += " and forum_id = " + params[:forumID]
end
@total, @topics = Topic.full_text_search(params[:searchTerms], :page
=> (params[:page]||1))
@total, @topics = Topic.full_text_search(params[:searchTerms], {:page =>
(params[:page]||1)}, {:conditions => @conditions})
@pages = pages_for(@total)
end
end
end
in my model Topic:
def self.full_text_search(q, options = {}, find_options = {})
return nil if q.nil? or q==""
default_options = {:limit => 10, :page => 1}
options = default_options.merge options
get the offset based on what page we’re on
options[:offset] = options[:limit] * (options.delete(:page).to_i-1)
now do the query with our options
results = Topic.find_by_contents(q, options, find_options)
puts options
puts find_options
return [results.total_hits, results]
end
i display 10 results in one page. In SearchController, if i run this
code:
@total, @topics = Topic.full_text_search(params[:searchTerms], :page =>
(params[:page]||1))
and input a search term, it will get 16 result messages. when i click to
the next page, it display the next 6 results, and the total_hits is
still 16.
but if i run this code in SearchController:
@total, @topics = Topic.full_text_search(params[:searchTerms], {:page =>
(params[:page]||1)}, {:conditions => @conditions})
and input the same search term, it will also get 16 result messages. but
when i click to the next page, it display the next 6 results, and the
total_hits changes to 6 messages, and only display one page.
speak in shortly: searching with conditions in find_by_contents, it got
16 total_hits, it display the first 10 result messages in first page,
when i click to the next page, it display the next 6 results, but the
total_hits changes to 6, and only got one page to display.
i don’t know why.