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? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 2/3/07, Johan <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > 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 happensThere''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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 ''!'') -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 2/3/07, Johan <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > 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 ''!'')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. :-) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---