# Has\_many and belons\_to Problem

**URL:** <https://www.ruby-forum.com/t/has-many-and-belons-to-problem/91597>\
**Category:** Rails\
**Created:** [April 10, 2007, 7:37pm UTC](https://www.ruby-forum.com/t/has-many-and-belons-to-problem/91597 "2007-04-10T19:37:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![physalias](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e36b37/32.png) [@physalias](https://www.ruby-forum.com/u/physalias)\
**Post date:** [April 10, 2007, 7:37pm UTC](https://www.ruby-forum.com/t/has-many-and-belons-to-problem/91597/1 "2007-04-10T19:37:46Z")

</div>

Dear all,

I have a problem with the relations of a model. The problem is the  
following.

I have 2 models. Page and PeContainer.  
Page:  
has\_and\_belongs\_to\_many :pe\_containers

PeContainer  
belongs\_to :page  
has\_many :pages

> Page  
> ----\>PeContainer  
> --------\>Page  
> ------------\>PeContainer

So my mainproblem is to get the parentpage of the PeContainer. I can  
only access the childpages. Can anyone help? Thanks in advance!

Kind regards  
Alex

---

<div class="post-metadata">

**Author:** ![physalias](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e36b37/32.png) [@physalias](https://www.ruby-forum.com/u/physalias)\
**Post date:** [April 10, 2007, 8:06pm UTC](https://www.ruby-forum.com/t/has-many-and-belons-to-problem/91597/2 "2007-04-10T20:06:53Z")

</div>

On 4/10/07, physalias [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ------------\>PeContainer  
> Hmm, that’s a very interesting relationship.

[1] Something to try first – and even if this is not part of the  
problem,  
It may help you in debugging – is to name the relationships more  
directly.  
In PeContainer, name them “parentpage” and “childpages”, etc.

[2] I think your problem might lie in the fact that in your Page model  
you  
have only declared one relationship, when really there are two. Consider  
the  
case of a Book, which has an author and an editor, both of which are  
People.  
You would declare the relationships seperately, you would not say “has  
many  
People”. Similarly, in this case you should HABTM :parent\_containers and  
HABTM :child\_containers.

Let us know if that gets you anywhere.

–  
John Joseph B.  
[http://lyceum.ibiblio.org](http://lyceum.ibiblio.org)  
[http://blog.johnjosephbachir.org](http://blog.johnjosephbachir.org)  
aim/yim/msn/jabber.org/gtalk: johnjosephbachir  
713-494-2704  
[http://jjb.cc](http://jjb.cc)

---

<div class="post-metadata">

**Author:** ![physalias](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e36b37/32.png) [@physalias](https://www.ruby-forum.com/u/physalias)\
**Post date:** [April 10, 2007, 8:07pm UTC](https://www.ruby-forum.com/t/has-many-and-belons-to-problem/91597/3 "2007-04-10T20:07:54Z")

</div>

> I have a problem with the relations of a model. The problem
> 
> > Page  
> > ----\>PeContainer  
> > --------\>Page  
> > ------------\>PeContainer
> 
> So my mainproblem is to get the parentpage of the  
> PeContainer. I can only access the childpages. Can anyone  
> help? Thanks in advance!

Looks to me like your page model is incorrect, judging from your text  
diagram. Assuming your model is a tree with alternating node types, you  
probably want:

Page:  
belongs\_to :pe\_container (parent)  
has\_many :pe\_containers (children)

PeContainer:  
belongs\_to :page (parent)  
has\_many :pages (children)

- donald

---

<div class="post-metadata">

**Author:** ![physalias](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e36b37/32.png) [@physalias](https://www.ruby-forum.com/u/physalias)\
**Post date:** [April 10, 2007, 8:10pm UTC](https://www.ruby-forum.com/t/has-many-and-belons-to-problem/91597/4 "2007-04-10T20:10:54Z")

</div>

On 4/10/07, Ball, Donald A Jr (Library) [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> belongs\_to :page (parent)  
> has\_many :pages (children)

Yes, Donald’s suggestion is better than mine 🙂

(although I would still name them more specifically)

–  
John Joseph B.  
[http://lyceum.ibiblio.org](http://lyceum.ibiblio.org)  
[http://blog.johnjosephbachir.org](http://blog.johnjosephbachir.org)  
aim/yim/msn/jabber.org/gtalk: johnjosephbachir  
713-494-2704  
[http://jjb.cc](http://jjb.cc)

---

<div class="post-metadata">

**Author:** ![physalias](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e36b37/32.png) [@physalias](https://www.ruby-forum.com/u/physalias)\
**Post date:** [April 10, 2007, 9:09pm UTC](https://www.ruby-forum.com/t/has-many-and-belons-to-problem/91597/5 "2007-04-10T21:09:05Z")

</div>

Dear all!

Thanks for fast answering.

I changed the Modelrelations to the settings Donald wrote. But this  
didn’t help.

I can’t rename my models, because I have an unlimited tree. So here is  
an example of what i do:

I have a page (id=1)  
This page has a container (id=1)  
This Container has 2 pages (id=2 & id=3)  
Page 1 has a container (id=2)  
Page 2 has a container (id=3)

So now I have to walk up the tree. So if I have the container (id=1) I  
need to get the parentpage (id=1), but I only can access the 2  
childpages. So is this issue resolvable?

Thanks in advance!

Kind regards  
Alex

On 10 Apr., 20:10, “John Joseph B.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![physalias](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e36b37/32.png) [@physalias](https://www.ruby-forum.com/u/physalias)\
**Post date:** [April 10, 2007, 8:08pm UTC](https://www.ruby-forum.com/t/has-many-and-belons-to-problem/91597/6 "2007-04-10T20:08:54Z")

</div>

On 4/10/07, physalias [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> So my mainproblem is to get the parentpage of the PeContainer. I can  
> only access the childpages. Can anyone help? Thanks in advance!

I just re-read your email and realized that I did not address the  
problem  
you stated :). But I think making your relationships more explicit is a  
good  
place to start.

–  
John Joseph B.  
[http://lyceum.ibiblio.org](http://lyceum.ibiblio.org)  
[http://blog.johnjosephbachir.org](http://blog.johnjosephbachir.org)  
aim/yim/msn/jabber.org/gtalk: johnjosephbachir  
713-494-2704  
[http://jjb.cc](http://jjb.cc)
