Pundit: NameError: uninitialized constant ApplicationPolicy

Created on 17 Sep 2017  路  1Comment  路  Source: varvet/pundit

Hi folks, apologies in advance if this is the wrong place to ask this question but after hitting a dead end while debugging, I thought I'd try here.

Whenever I try to deploy my app to Heroku or through a CI, I get the following error on one of my policy files.

NameError: uninitialized constant ApplicationPolicy

Now, the problem is that I don't see this NameError on my local development box. It would seem to suggest that somehow the path for my Pundit policies isn't getting loaded on Heroku but are happening on local box. But I am not sure why:

Here is the relevant part of my application.rb looks like:

config.autoload_paths << Rails.root.join('lib')
Dir[Rails.root.join('app/policies/*.rb')].each &method(:require)

A typical policy file line where the error occurs:

class UserSettingPolicy < ApplicationPolicy

All the policy files including ApplicationPolicy live under app/policies/. My guess is that the config/application.rb code that should somehow be more robust to autoload all policies?

Any ideas on what might be going on would be super appreciated! And if this makes for a helpful tip for anyone else, I'd be happy to make a PR!

Most helpful comment

Found the problem finally. The policy files were getting loaded one by one in the above code and hence had to change it to below for Rails to load entire directory at once it seems.

config.autoload_paths += Dir[Rails.root.join('app', 'policies', '*.rb')]
# earlier it was Dir[Rails.root.join('app/policies/*.rb')].each &method(:require)

Sorry for the noise, but hope it helps someone else too!

>All comments

Found the problem finally. The policy files were getting loaded one by one in the above code and hence had to change it to below for Rails to load entire directory at once it seems.

config.autoload_paths += Dir[Rails.root.join('app', 'policies', '*.rb')]
# earlier it was Dir[Rails.root.join('app/policies/*.rb')].each &method(:require)

Sorry for the noise, but hope it helps someone else too!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mackshkatz picture mackshkatz  路  4Comments

ucarion picture ucarion  路  4Comments

mattruzicka picture mattruzicka  路  5Comments

cthielen picture cthielen  路  5Comments

epinault picture epinault  路  6Comments