Hi Guys, I have a mail_templates table I have a mailinglists table I have a mail_templates_mailinglists table with mailtemplate_id, and mailinglist_id mail_template has_and_belongs_to_many :mailinglists mailinglist has_and_belongs_to_many :mail_templates, :foreign_key => "mailinglist_id" I want to be able to stop the user being able to destroy the mail_template if it is being used by a mailinglist. So in my destroy method in my mail_template controller I want to find any mailinglists that have a mail_template id. I tried the following but I am getting an error, <code> mailinglist = Mailinglist.mail_template_mailinglists.find(:all, :conditions => ["mail_template_id = ?", params[:id]]) if mailinglist.empty? MailTemplate.find(params[:id]).destroy else flash[:notice] = ''Cannot delete mail template, as it is being used by one or more mailinglist.'' end </code> NoMethodError in Admin/mail templatesController#destroy undefined method `mail_template_mailinglists'' for #<Class:0x3333814> -- 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 -~----------~----~----~----~------~----~------~--~---
Dave Smith wrote:> <code> > mailinglist = Mailinglist.mail_template_mailinglists.find(:all, > :conditions => ["mail_template_id = ?", params[:id]]) > > if mailinglist.empty? > MailTemplate.find(params[:id]).destroy > else > flash[:notice] = ''Cannot delete mail template, as it is being used > by one or more mailinglist.'' > end > </code> > > > NoMethodError in Admin/mail templatesController#destroy > > undefined method `mail_template_mailinglists'' for #<Class:0x3333814>any ideas anyone? -- 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 Mon, Jan 19, 2009 at 1:01 PM, Dave Smith < rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Hi Guys, > > I have a mail_templates table > I have a mailinglists table > I have a mail_templates_mailinglists table with mailtemplate_id, and > mailinglist_id > > mail_template has_and_belongs_to_many :mailinglists > mailinglist has_and_belongs_to_many :mail_templates, :foreign_key => > "mailinglist_id" > > I want to be able to stop the user being able to destroy the > mail_template if it is being used by a mailinglist. > > So in my destroy method in my mail_template controller I want to find > any mailinglists that have a mail_template id. > > I tried the following but I am getting an error, > > <code> > mailinglist = Mailinglist.mail_template_mailinglists.find(:all, > :conditions => ["mail_template_id = ?", params[:id]]) > > if mailinglist.empty? > MailTemplate.find(params[:id]).destroy > else > flash[:notice] = ''Cannot delete mail template, as it is being used > by one or more mailinglist.'' > end > </code> > > > NoMethodError in Admin/mail templatesController#destroy > > undefined method `mail_template_mailinglists'' for #<Class:0x3333814> >You''re trying to invoke a class method ''mail_template_mailinglists'', which isn''t how habtm works. What you want is to retrieve the MailTemplate object, and ask it whether it has any mailinglists. Try this (not tested, but should give you how it works): <code> mail_template = MailTemplate.find(params[:id]) # grab the MailTemplate we''re looking to destroy if mail_template.mailinglists.empty? # see whether it has any mailinglists using it mail_template.destroy else flash[:notice] = "Cannot delete mail template, as it is being used by one or more mailinglists." end </code> 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 -~----------~----~----~----~------~----~------~--~---