I have a variable that I obtain through my controller. I want to check and see if there’s a record with that variable’s value in the DB. If there is, then delete that record. In my User model there’s a ‘number’ attribute that’s a string. Here’s what the pseudo code in my controller would look like:
from_number = params["From"]
if @user.number == from_number
# find a record in the DB with that value and delete it
User.find(from_number)
@user.destroy
else
puts "don't delete DB record"
end
Can I do this in the controller or should this be done in the model? If it should be done in the model, how do I pass from_number to the model to process?
Thanks!
Use the code given below, it will delete existing user in the db and call all callbacks on this instance
So no need to write “if else”. And this code should be placed in controller only.