Home > front end >  Ruby On Rails: Disable `delete_all` when table name is present
Ruby On Rails: Disable `delete_all` when table name is present

Time:05-10

delete_all is useful, but I never want to see it called on the same line with a table name. I'd like to disable things like TableName.destroy_all in both console and code.

One interesting issue happened earlier this month:

Application.destroy_all was called on a model instead of applications.destroy_all (the model has_many applications)

For somebody new to ROR, it looks very similar, but the results were disastrous.

I'm open to some form of lint/code style tool, but that really wouldn't catch it in the console scenario. (Plus, I haven't been able to get rubo-cop to do something like this yet)

Basically, I'm asking for a way to make the console and codebase more secure so that newer developers can't inadvertantly delete everything in a table.

CodePudding user response:

I'm not entirely clear on what you are trying to accomplish, but you could try overriding the method in your ApplicationModel with something like this (assuming Rails 5 or greater, or otherwise a root model in existence).

class ApplicationModel < ActiveRecord::Base
  def self.destroy_all(*args)
    raise('Cannot destroy all records of a model this way. Did you mean to delete a subset of records instead?')
  end
end

Possibly make this method private if you'd like it even harder to run...

def self.destroy_all(*args)
  raise('Cannot destroy all records of a model this way. Did you mean to delete a subset of records instead?')
end
private_class_method :destroy_all

You could get fancy and allow this to be bypassed with a special argument that you check for, but give this a try and see how it goes.

  • Related