0

I m working in rails.i want to call a function(show) of class "twitter" in searcht1 which is in "lib" within a same directory from rake task.

lib/task/search.rake:                                                                                                           

require_relative "lib/Searcht1"      
  namespace :Searcht do
   task :search => :environment do
     obj=Twitter.new
     obj.show
   end  
  end

and the class inside the "searcht1.rb" is like this

class Twitter
 def show
  "anylogic"
 end
end

but it is not calling that function.and i m not getting any result. greatly aprreciate any help

1 Answers1

0

I know this question is a bit outdated, but I had the problem in my rails5 app and this is how I keep my tasks clean :)

Please make a subdir searchdir in lib/tasks => lib/tasks/searchdir and put your referenced file searcht1.rb in there. Now you can reference it in your rake file.

Please keep in mind: it is run from the referenced file.

# in lib/tasks/search.rake                                                                                                           

namespace :Searcht do
 task :search => :environment do
   require_relative "searchdir/searcht1"      
 end  
end

make your code run here:

# in lib/tasks/searchdir/searcht1.rb
class Twitter
  def show
    "anylogic"
  end
end

obj=Twitter.new
obj.show