# Has\_and\_belongs\_to\_many DRY?

**URL:** <https://www.ruby-forum.com/t/has-and-belongs-to-many-dry/132636>\
**Category:** Rails\
**Created:** [March 22, 2008, 9:16am UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-dry/132636 "2008-03-22T09:16:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [March 22, 2008, 9:16am UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-dry/132636/1 "2008-03-22T09:16:09Z")

</div>

Hi,

I have many “has\_and\_belongs\_to\_many” that looks nearly the same, I  
wonder if I can do anything so I DRY myself? maybe write a plugin? What  
do you think?

The lines look like this.

has\_and\_belongs\_to\_many :snippets,  
:class\_name =\> ‘Container::Snippet’,  
:conditions =\> [ “containers.reference\_type =  
‘Layout’ AND containers.container\_type = ‘Snippet’” ]  
:join\_table =\> :containers,  
:foreign\_key =\> :reference\_id,  
:association\_foreign\_key =\> :container\_id

has\_and\_belongs\_to\_many :modules,  
:class\_name =\> ‘Container::Module’,  
:conditions =\> [ “containers.reference\_type =  
‘Layout’ AND containers.container\_type = ‘Module’” ]  
:join\_table =\> :containers,  
:foreign\_key =\> :reference\_id,  
:association\_foreign\_key =\> :container\_id

has\_and\_belongs\_to\_many :texts,  
:class\_name =\> ‘Container::Text’,  
:conditions =\> [ “containers.reference\_type =  
‘Layout’ AND containers.container\_type = ‘Text’” ]  
:join\_table =\> :containers,  
:foreign\_key =\> :reference\_id,  
:association\_foreign\_key =\> :container\_id

Hope someone have a solution, thanks for help 🙂

Regards,  
Jamal

---

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [March 22, 2008, 10:51am UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-dry/132636/2 "2008-03-22T10:51:42Z")

</div>

I just did simple plugin for this 🙂

Thanks anyway 😃
