search with condition on sunspot

68 views
Skip to first unread message

hyperrjas

unread,
May 22, 2012, 1:19:44 PM5/22/12
to Sunspot
I have this in my post.rb

#search
searchable do
text :title, :description, :boost => 2.0
time :created_at
string :language
end

I have this action for in my posts_controller.rb:

def index
@search = Post.solr_search do |s|
s.fulltext params[:search]
s.keywords params[:search]
s.order_by :created_at, :desc
s.paginate :page => params[:page], :per_page => 1

end
@posts = @search.results
).page(params[:page]).per(20)
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @posts }
format.json { render :json => @posts }
end
end

Post model have an attribute language inside search block.

I want get in search results only the posts with @post.language ==
"#{I18n.locale.to_s}"

I18.locale.to_s is the current language for visitor. current language
is a string like "en", "es", "us"...etc

How can I do it?

Thank you!

Andy Lindeman

unread,
May 22, 2012, 4:26:31 PM5/22/12
to ruby-s...@googlegroups.com
On Tue, May 22, 2012 at 12:19 PM, hyperrjas <hype...@gmail.com> wrote:
> I want get in search results only the posts with @post.language ==
> "#{I18n.locale.to_s}"

I skimmed the email, but I think you want Post.search {
with(:language, I18n.locale.to_s) }

--
Andy Lindeman
http://www.andylindeman.com/

hyperrjas

unread,
May 22, 2012, 5:49:09 PM5/22/12
to Sunspot
Thank you very much. I have fixed this problem with:

if params[:locale].present?
s.with(:language, params[:locale])
else
params[:locale] = I18n.locale
s.with(:language, params[:locale])
end

Thank you very much for your fix :). It does works fine too :D.



On May 22, 1:26 pm, Andy Lindeman <alinde...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages