Austin
Austin

Reputation: 3890

How to undefine/remove a method from a Rails association

When you define an association in Rails, it creates some methods on that association for you. For example, consider the following:

class Person
  has_many :friends
end

One of the methods you get is person.friends.clear

How can I remove this method such that person.friends.clear would raise a NoMethodError?

Note: I have tried a few things, including undef_method and remove_method without luck. Please only provide known answers and not guesses.

Upvotes: 1

Views: 110

Answers (1)

Samiron
Samiron

Reputation: 5317

Though I didnt try it myself but could would pls check if the following works

has_many :friends do
    def clear 
       raise NoMethodError, "Some reason text"
    end     
end

Upvotes: 2

Related Questions