# Has\_and\_belongs\_to\_many polymorphic question

**URL:** <https://www.ruby-forum.com/t/has-and-belongs-to-many-polymorphic-question/111414>\
**Category:** Rails\
**Created:** [August 7, 2007, 3:57am UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-polymorphic-question/111414 "2007-08-07T03:57:48Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christopher\_Goellne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/cab0a1/32.png) [@Christopher\_Goellne](https://www.ruby-forum.com/u/Christopher_Goellne)\
**Post date:** [August 7, 2007, 3:57am UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-polymorphic-question/111414/1 "2007-08-07T03:57:48Z")

</div>

I’ve got a situation where I’m trying to model the following  
relationship:

a ‘page’ has multiple lineitems

a ‘lineitem’ can be either a ‘page’ or a ‘entity’

The ‘lineitem’ looks like it should be polymorphic but the ‘page’ and  
‘lineitem’ relationship has me confused. It looks like a  
has\_and\_belongs\_to\_many but a ‘lineitem’ can only exist on one page  
and can refer to any single ‘page’ or ‘entity’.

If I do has\_and\_belongs\_to\_many on both sides isn’t that a two way  
multiple association?

Basically, the model is like a filesystem where a directory can have  
multiple files or other directories, etc. I’m keeping the lineitem  
around because I need a way to order the lineitems on the page.

I’ve looked at the folder example on the wiki but it doesn’t link  
folders back into folders.

Any suggestions?

Thanks.
