Has_and_belongs_to_many polymorphic question

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.