-1

So rake db:reset clears the database, but how do I do that in a controller?

  • Why would you want this? Cleaning the database ain't usually a standard practice. Do you mean, there will be a feature (say a link) when clicked should invoke the Rails Controller and it should clean the entire DB? I see serious security concerns with this kind of control. The DB-admins should rather manage this. – Satya Kalluri Dec 03 '13 at 08:50
  • OK I searched for 2 days and didn't find that, I was searching for how to clear the database using the controller. But that answer says not to use rake commands in a controller. So how do I set up my controller to clear the database? – user1780775 Dec 03 '13 at 08:51
  • 3
    are you afraid they won't pay you? – Miotsu Dec 03 '13 at 08:59

1 Answers1

1

I think you might be misunderstanding what a controller does. Check out the official Rail's guide.

Action controller is the C in MVC. After routing has determined which controller to use for a request, your controller is responsible for making sense of the request and producing the appropriate output.

Assuming you wanted to delete all the records for a particular model, you could do something like:

class ModelNameController < ApplicationController
  def delete_all
    ModelName.delete_all # This also accepts optional conditions.
  end 
end

Keep in mind if you want to clean the database, that isn't the job of the controller.

sunnyrjuneja
  • 6,033
  • 2
  • 32
  • 51