Rails : Cannot include PgSearch Module provided by the pg_search Gem
Asked Answered
P

1

6

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.

Plainclothesman answered 26/7, 2013 at 23:57 Comment(4)
Having similar issues - have you found a solution yet?Townsville
No, I have switched to Sphinx and thinking_sphinx.Plainclothesman
Had similar issues... Restarted the server and things started working.Imperishable
Restart the server. It worked for me locally.Marte
S
3

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

It is easy to forget a step.

Selfridge answered 28/10, 2016 at 11:59 Comment(2)
THANK YOU! I've been troubleshooting this for 30 minutes now and had forgotten to restart my rails server. Duh. Much appreciated.Nameplate
In my case, I was working inside the rails console. I needed to quit the console and restart it.Retroflex

© 2022 - 2024 — McMap. All rights reserved.