# Quick answer needed on dependancy

**URL:** <https://www.ruby-forum.com/t/quick-answer-needed-on-dependancy/158143>\
**Category:** Rails\
**Created:** [January 19, 2009, 8:01pm UTC](https://www.ruby-forum.com/t/quick-answer-needed-on-dependancy/158143 "2009-01-19T20:01:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dnstevenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8dc957/32.png) [@dnstevenson](https://www.ruby-forum.com/u/dnstevenson)\
**Post date:** [January 19, 2009, 8:01pm UTC](https://www.ruby-forum.com/t/quick-answer-needed-on-dependancy/158143/1 "2009-01-19T20:01:44Z")

</div>

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,

`
  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
`

NoMethodError in Admin/mail templatesController#destroy

undefined method `mail\_template\_mailinglists’ for #Class:0x3333814

---

<div class="post-metadata">

**Author:** ![dnstevenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8dc957/32.png) [@dnstevenson](https://www.ruby-forum.com/u/dnstevenson)\
**Post date:** [January 19, 2009, 9:03pm UTC](https://www.ruby-forum.com/t/quick-answer-needed-on-dependancy/158143/2 "2009-01-19T21:03:23Z")

</div>

Dave S. wrote:

> `
> 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
> `
> 
> NoMethodError in Admin/mail templatesController#destroy
> 
> undefined method `mail\_template\_mailinglists’ for #Class:0x3333814

any ideas anyone?

---

<div class="post-metadata">

**Author:** ![dnstevenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8dc957/32.png) [@dnstevenson](https://www.ruby-forum.com/u/dnstevenson)\
**Post date:** [January 19, 2009, 9:40pm UTC](https://www.ruby-forum.com/t/quick-answer-needed-on-dependancy/158143/3 "2009-01-19T21:40:05Z")

</div>

On Mon, Jan 19, 2009 at 1:01 PM, Dave S. \<  
removed\_email\_address@domain.invalid\> wrote:

> “mailinglist\_id”  
> mailinglist = Mailinglist.mail\_template\_mailinglists.find(:all,
> 
> 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):

`
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
`

HTH!
