In our rails 3.2.12
app, there is a rake task
created under lib/tasks
. The rake task
needs to call a method find_config()
which resides in another rails module authentify
(module is not under /lib/). Can we include Authentify
in rake task
and make method find_config()
available to call in the rake task?
Here is what we would like to do in the rake task
:
include Authentify
config = Authentify::find_config()
Thanks for comments.
Module file exist her
app/lib/module/sub_module.rb
like-and my rake_task presented here
/lib/tasks/my_tasks.rake
as-from app directory not from
rails console
That worked for me !!!
This works for me. Since your Module is not in /lib you might have to edit how it is required. But it should work. Hope it helps.
How to require a Rails service/module in a Rake task?
I had the same problem and manage to solve it by requiring the rails files inside the rake task.
I had a rake task named
give_something
defined in the filelib/tasks/a_task.rake
.Within that task I needed to call the function
give_something
from the moduleAService
which lives in the fileapp/services/a_service.rb
The rake task was defined as follows:
I was getting the error:
uninitialized constant AService
To solve it I had to require the module not at the beginning of the file
a_task.rake
, but inside the rake task: