0

I have been trying to import a .rake file into my controller using import 'file.rake' but I seem to get an error saying undefined method 'import' for Main:object. I have tried to look online for any documentation on how to use import but I can't find anything. Does anyone have a solution or have come across this error before?

Thanks in advance.

thedarkknight228
  • 535
  • 1
  • 5
  • 9
  • Could you give a little more context to what you're trying to achieve? – Matt Jun 04 '13 at 08:57
  • Can you explain in more detail what you trying to do, because loading a rake file in the controller looks suspicious. – Paulo Fidalgo Jun 04 '13 at 08:57
  • On top of which, `import` isn't a method in Ruby's core - it exists in the `Rake` library to allow rakefile includes. – Chowlett Jun 04 '13 at 09:03
  • My controller controls my form that I fill out and I am trying to activate the .rake file once the controller is finished, also I will be passing an id of the new customer to the .rake file from the controller telling the .rake file its a new customer. So I was trying to import the .rake file into the controller so I could pass the id to it and run it. – thedarkknight228 Jun 04 '13 at 09:03
  • What is your rakefile doing? It sounds like you might be trying to do something in a very unusual way, and there's probably a better way. – Chowlett Jun 04 '13 at 09:04
  • My rake file is navigating to a website and logging into that website using the information from the controller and depending on whether its a new customer or an existing one it downloads certain files from that website. If new customer is goes back 2 years and gets the data if existing customer it only downloads one days of data. The controller populates the database with the credentials of the new customer and I want to the .rake file to automatically run once the form is filled out so it downloads the customers data for them – thedarkknight228 Jun 04 '13 at 09:08
  • If you are wanting a background task, you should look into Sidekiq, Resque, or DelayedJobs. Rake tasks should not be run through your website -- they are maintenance tasks. – Jesse Wolgamott Jun 04 '13 at 12:56

2 Answers2

0

I think you're probably going about this the wrong way. Why is that functionality in a Rakefile?

Rake is Ruby's equivalent of make - it's designed to handle software tasks with dependencies, like building an app, or packaging an releasing a Rubygem. Even Wikipedia has a pretty good description of what it's for, or see this answer for a link dump.

What you've described sounds like it's normal Rails stuff - take web input, act on it, interface with database. I'd seriously consider rewriting your Rake tasks inside Rails.

Community
  • 1
  • 1
Chowlett
  • 45,935
  • 20
  • 116
  • 150
0

There are a couple of ways you can read about here: Run rake task in controller

But I really wouldn't recommend them. If you are finding a need to run some extra code within your application, breaking out to the console is not the way to do it. Write the code into your application, perhaps in a lib file, and call that instead.

You can even have your rake file call the same lib method so that you're DRY.

Add a code file to the lib folder, put all the logic in there

module ExtraActions
  def method_that_does_logins
    ...
  end
end

Include this in your controller:

UsersController < ApplicationController
  include ExtraActions

  def create
    ... #normal code
    method_that_does_logins
  end
end
Community
  • 1
  • 1
Matt
  • 13,948
  • 6
  • 44
  • 68