rtacconi
rtacconi

Reputation: 14789

Override destroy and delete in Rails 3 and removing the callbacks

I want to block destroy and delete in the User model only and I want to block the callback. In few words I want to block any possibility to delete a user record. I tried by overriding destroy and delete methods but they are not called and the user is alway deleted.

class User < ActiveRecord::Base
  def destroy
  end

  def delete
  end
end

I do not want to use any gem related to this, so do not cite any gem. I am not sure if overriding those methods is a good idea, probably I should just create a 'deactivate' method and leave the default delete and destroy method there, just in case I will need them in the Rails console

Upvotes: 1

Views: 1525

Answers (2)

Joshua Pinter
Joshua Pinter

Reputation: 47621

Refer to Rails API Methods.

It's not necessarily a bad thing to override these methods provided you know what you're doing and the consequences. Sometimes it's the cleanest way to solve a problem.

My recommendation is looking at the following Rails API method documentation entries and ensuring you're not missing any important actions here.

#destroy

#destroy!

#delete

By creating those methods in your model, it will override anything in the inherited Rails API so it should work just fine.

Upvotes: 1

Baruch
Baruch

Reputation: 1133

I'm not sure that I understand your question fully.

One way to do this is to make the destroy and delete methods private.

You say that the methods never get called, but that the objects are being deleted anyway. How does this happen? Are you deleting them from a collection? You may need to override a class method of User rather than those two instance methods.

Upvotes: 0

Related Questions