Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails : Cannot include PgSearch Module provided by the pg_search Gem

TO SUM UP :

The module PgSearch provided by the Gem pg_search cannot be included, required or loaded on the staging environment (Rbenv, nginx, unicorn,capistrano), the problem happens on the web server through http but does not appear on the staging server's rails command. An other module provided by an other gem can be included without error.

No problem on the local development environment (rvm, puma).

DETAILS

I am currently developing a Ruby On Rails 4.0 application with ruby 2.0.0 which git repositories are hosted on bitbucket. I deploy the app through a staging server using capistrano.

  • Staging server environment : rbenv, nginx and unicorn
  • Local development environment : rvm and puma

The rails environnment files (environment/production.rb & environment/staging.rb) for both are the same.

WHAT DID I DO :

I have installed the pg_search gem (a PostgreSQL full text search gem) by adding it to my Gemfile and put the clause "include PgSearch" in the Active Record model I wanted to use with pg_search gem

I have run the app in development mode... it works !

PROBLEM :

After having deployed the changes to the staging server : Through the http server I get this error :

NameError in App::MyController#index Uninitialized constant MyActiveRecordModel::PgSearch

(Normally, this pg_search gem which is included in the GemFile should have its lib/*.rb files included in the autoload search path and a clause like load "pg_search.rb", require"pg_search" or "include PgSearch" (module included in pg_search.rb file) should pass.

In order to find clues to fix the bug, I have :

-tried if an other module provides by the gem could be included ... It works

After been to the current release path of the staging server I run "bundle exec rails c staging" and tried to :

  • see if the ActiveRecord Model ( which I included PgSearch) instanciation works.

  • see if the Module provided by the gem could be found / loaded on the server and I have executed - include PgSearch and require "pg_search" and load "pg_search.rb".

All these commands were a succeess.

I run out of ideas to find some other clues, would you have any suggestions please ?

Thank you.

like image 496
lagrangemartin Avatar asked Nov 02 '22 17:11

lagrangemartin


1 Answers

Restart the rails server

Gems introduce new code that rails will need to access. To make the new code available to Rails we go through 3 steps:

  1. Change the Gemfile
  2. bundle install
  3. Restart the rails server

    • When do I need to restart the rails server?

It is easy to forget a step.

like image 82
notapatch Avatar answered Nov 09 '22 14:11

notapatch