简体   繁体   中英

How to include rails module in lib folder inside sidekiq worker class

I'm trying to call a method in a utility module I have in a rails 4 project. I use the module throughout my rails project for some activerecord behaviour, I recently added sidekiq to the project to perform some customer analysis on a daily basis but I can't seem to get the sidekiq worker to include the module at all.

This is what I have in the worker file:

require '../lib/utils/customer_utils.rb'

the location of the utility file is in:

lib/utility  

And the module is written like so:

module CusomterUtils

def get_average_spend
..
end
...

Any idea on what I should be doing?

Thanks

I recommend following the Rails autoloading convention names , so either of these should work:

# lib/utils/customer_utils.rb
module Utils
  class CustomerUtils
    ...
  end
end

or

# lib/customer_utils.rb
class CustomerUtils
  ...
end

Typically the folder names are namespaces and the file name is the class name.

Note that the naming conventions changed over the versions of Rails, so lib may not be automatically loaded without a specific configuration option.

You could also use require_relative '../lib/utils/customer_utils.rb' .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM