Search code examples
ruby-on-rails-3.1sunspotrails-enginessunspot-solr

Sunspot/Solr raketasks not loading in Rails 3 Mountable Engine


I'm trying to add the sunspot_rails gem to my Rails Mountable Engine, so I can use Solr to do full text searches. Like it states in the README file I've added this to my Gemfile:

gem "sunspot_rails"
gem "sunspot_solr"

Then I run rails g sunspot_rails:install which creates sunspot.yml in the config folder of my Rails Engine. To start sunspot I need to run:

bundle exec rake sunspot:solr:start

But that doesn't work, and gives me the following error message:

rake aborted!
Don't know how to build task 'sunspot:solr:start'

It seems that it doesn't load the Sunspot/Solr rake tasks, and so it can't find them. I've had other issues with the Rails engine not loading files that would load automatically in a normal Rails 3 application. I suspect something along that line is going on here as well. I checked the lib/tasks folder, and there is only one file in there: my_app.rake. In that file there are only a few commented lines of code:

# desc "Explaining what the task does"
# task :my_app do
#   # Task goes here
# end

I think I would maybe need to add the raketasks manually, and load them from my_app.rake. However, I can't find any information on how to do this, and I maybe I'm completely wrong in the first place. Hopefully someone on stackoverflow has experience with this.

In any case, thank you very much for any help in advance!


Solution

  • A few days ago found a solution to this problem myself, so for the sake of completeness and (hopefully) to be of help to others, I will post my solution here.

    The problem actually occurs because a Rails 3 Mountable Engine behaves a lot LIKE a normal Rails app, but also has a lot of minor/major differences. You just have to run bundle exec rake app:sunspot:solr:start instead of just bundle exec rake sunspot:solr:start.

    So to get Sunspot working in a Rails 3 Mountable Engine, I followed these steps:

    Add to Gemfile

    # Gemfile (Don't forget to move them to your gemspec when packaging your engine)
    
    gem 'sunspot_solr' #only for development mode
    gen 'sunspot_rails'
    

    After that, don't forget to run bundle install to install the gems.

    Configure your engine to use Sunspot/Solr

    To generate the needed config/sunspot.yml file run:

    rails generate sunspot_rails:install
    

    Start Solr on your local machine

    Finally, to start a local instance of Solr run:

    bundle exec rake app:sunspot:solr:start
    

    And now you are all set!