# How to delete a record with form data

**URL:** <https://www.ruby-forum.com/t/how-to-delete-a-record-with-form-data/84330>\
**Category:** Rails\
**Created:** [February 3, 2007, 8:12am UTC](https://www.ruby-forum.com/t/how-to-delete-a-record-with-form-data/84330 "2007-02-03T08:12:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![johan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dec6dc/32.png) [@johan](https://www.ruby-forum.com/u/johan)\
**Post date:** [February 3, 2007, 8:12am UTC](https://www.ruby-forum.com/t/how-to-delete-a-record-with-form-data/84330/1 "2007-02-03T08:12:58Z")

</div>

Hi fellow RoR’ers,

I have a Rails app, where a user can subscribe and unsubscribe from a  
mailinglist. To unsubscribe, the user can fill in their email address  
in a form field.

But I have no idea how to delete the record from the database, using  
the typed in data from the user. I’ve tried it with the following  
code, but doesn’t seem to work.

@email = Email.find(:all, :conditions =\> [“mail = ?”, params[:mail]])  
@email.delete(:id)

–\> no error, but nothing happens

Or this way, didn’t work either,

Email.find\_by\_mail(params[:email][:mail]).destroy!

–\> then I got the following error: undefined method `find\_by\_mail’ for  
Email:Class

Any ideas?

---

<div class="post-metadata">

**Author:** ![johan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dec6dc/32.png) [@johan](https://www.ruby-forum.com/u/johan)\
**Post date:** [February 3, 2007, 8:55am UTC](https://www.ruby-forum.com/t/how-to-delete-a-record-with-form-data/84330/2 "2007-02-03T08:55:08Z")

</div>

On 2/3/07, Johan [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> @email = Email.find(:all, :conditions =\> [“mail = ?”, params[:mail]])  
> @email.delete(:id)
> 
> → no error, but nothing happens

There’s no instance method #delete, AFAIK. What does @email contain?  
Should you be using be “params[:email][:mail]” instead? A shorter way  
to do it would be with Email.destroy\_all, or, if you’re really sure,  
Email.delete\_all.

> Or this way, didn’t work either,
> 
> Email.find\_by\_mail(params[:email][:mail]).destroy!
> 
> → then I got the following error: undefined method `find\_by\_mail’ for  
> Email:Class
> 
> Any ideas?

I’d expect #find\_by\_mail to work so long as:

- Email derives from ActiveRecord::Base, and
- there is a column called ‘mail’ on the corresponding table.

Which leads me to two questions:

- Does your class declaration start with “class Email \<  
ActiveRecord::Base”?
- Have you migrated your database appropriately so your ‘emails’ table  
(unless  
you’ve used set\_table\_name) contains a column called ‘mail’?

HTH.

---

<div class="post-metadata">

**Author:** ![johan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dec6dc/32.png) [@johan](https://www.ruby-forum.com/u/johan)\
**Post date:** [February 3, 2007, 9:29am UTC](https://www.ruby-forum.com/t/how-to-delete-a-record-with-form-data/84330/3 "2007-02-03T09:29:26Z")

</div>

Aha,

Seems to work now, but without the ‘!’ at the end after destroy.

Email.find\_by\_mail(params[:email][:mail]).destroy

Thanks for the help! (with a ‘!’)

---

<div class="post-metadata">

**Author:** ![johan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dec6dc/32.png) [@johan](https://www.ruby-forum.com/u/johan)\
**Post date:** [February 3, 2007, 10:24am UTC](https://www.ruby-forum.com/t/how-to-delete-a-record-with-form-data/84330/4 "2007-02-03T10:24:15Z")

</div>

On 2/3/07, Johan [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks for the help! (with a ‘!’)  
> I’m happy it worked for you, but if it was me, I’d certainly be  
> digging down deeper. “Oh it just works now” doesn’t usually satisfy  
> me as a developer. Calling #destroy instead of #destroy! shouldn’t  
> make a fleck of difference as to whether or not Email responds to  
> #find\_by\_mail. Y’know?

It’s up to you. You’re welcome either way. 🙂
