How can I use rsolr in rails app

185 views
Skip to first unread message

m00nlight

unread,
Jun 13, 2012, 3:02:35 AM6/13/12
to rsolr
Hello, everyone,
I am a new guy to rails, and I want to use rsolr in rails app to do
search. I already have indexed the seach document in solr and I can
search with rsolr in ruby program. But when I want to add rsolr in my
rails app,
it gives the error message :

uninitialized constant Post::RSolr

Does anyone have any example how to use rsolr in rails. Thanks :)

matt mitchell

unread,
Jun 24, 2012, 4:31:27 PM6/24/12
to rs...@googlegroups.com
Hi m00nlight,

RSolr is a top-level constant, it doesn't belong in the Post class. What you want to do is access it like this:

require "rsolr"

class Post < Xyz
  def some_method
    connection = RSolr.connect
    response = connect.select :params {:q => "*:*"}
    # .. etc..
  end
end

Also, you'd normally want to define your connection somewhere else, maybe a constant within your apps namespace?

- Matt

m00nlight

unread,
Jun 25, 2012, 12:44:44 AM6/25/12
to rs...@googlegroups.com
Hi Matt,

Thanks for your kindly help. It now works. It seems that when I restart
the rails server, the error message disappear.
I don't know why. Because before, I only need to change the code but does
not to restart the rails server, the code will
take effect. Thanks for your kindly help. :)

Yours sincerely
m00nlight

On Mon, 25 Jun 2012 04:31:27 +0800, matt mitchell <good...@gmail.com>
wrote:
--
Best Regards
Using Opera's revolutionary email client: http://www.opera.com/mail/
Reply all
Reply to author
Forward
0 new messages