# Is there a "maybe\_belongs\_to"?

**URL:** <https://www.ruby-forum.com/t/is-there-a-maybe-belongs-to/129095>\
**Category:** Rails\
**Created:** [February 14, 2008, 7:11am UTC](https://www.ruby-forum.com/t/is-there-a-maybe-belongs-to/129095 "2008-02-14T07:11:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kopf1988](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/74df32/32.png) [@kopf1988](https://www.ruby-forum.com/u/kopf1988)\
**Post date:** [February 14, 2008, 7:11am UTC](https://www.ruby-forum.com/t/is-there-a-maybe-belongs-to/129095/1 "2008-02-14T07:11:04Z")

</div>

Hi, I’m working on a project to register people for events, and was  
looking for a little help on it.

I’ve got this class “Events”. Basically, there are going to be several  
big events throughout time, and those events should not belong to  
anybody/anything. But then, we’ve got sub events. For example, someone  
could go to Event “HorseShow” and then go to Event “RidingClass”, part  
of the horse show. What should I do to make that RidingClass belong to  
another event, but not the HorseShow.

Also, Person is going to be attending HorseShow, and also RidingClass,  
so I’ll have to deal with that… probably has many through. But  
has\_many :events, :through =\> :events probably doesn’t work.

Thanks in advance for your posts!

-Ryan

---

<div class="post-metadata">

**Author:** ![kopf1988](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/74df32/32.png) [@kopf1988](https://www.ruby-forum.com/u/kopf1988)\
**Post date:** [February 14, 2008, 11:29am UTC](https://www.ruby-forum.com/t/is-there-a-maybe-belongs-to/129095/2 "2008-02-14T11:29:27Z")

</div>

> has\_many :events, :through =\> :events probably doesn’t work

it does, if Events has events\_id but needs another name:  
has\_many :events # for the normal relation  
has\_many :sub\_events, :through =\> :events

if events\_id is nil, it doesn’t belong to another event (like for  
HorseShow)

but i would keep it in the events model depending on, how deep your  
events may be nested

you may have a look at this  
[http://wiki.rubyonrails.org/rails/pages/ActsAsTree](http://wiki.rubyonrails.org/rails/pages/ActsAsTree)

works just the same, only they use parent\_id  
would look like this then:

in Events:  
has\_many :child\_events, :foreign\_id =\> :parent\_id
