# Database relationships question

**URL:** <https://www.ruby-forum.com/t/database-relationships-question/193922>\
**Category:** Rails\
**Created:** [August 26, 2010, 4:20pm UTC](https://www.ruby-forum.com/t/database-relationships-question/193922 "2010-08-26T16:20:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jooma2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9bcb4/32.png) [@jooma2](https://www.ruby-forum.com/u/jooma2)\
**Post date:** [August 26, 2010, 4:20pm UTC](https://www.ruby-forum.com/t/database-relationships-question/193922/1 "2010-08-26T16:20:20Z")

</div>

Hello.

For fun, I’m making a text based web MMO.

I have the following tables to consider.

characters  
quests  
objectives

So right now my quests can have many objectives (ie: kill 10 wolves,  
kill pack leader).

So that’s easy enough.  
But many characters can have the same quests. So I made

characters\_quests

Ok that’s fine. But since quests have multiple objectives that I need to  
track how do I do this?

characters\_quests\_objectives ?

I don’t know why but I can’t wrap my head around this relationship  
right now.

---

<div class="post-metadata">

**Author:** ![jooma2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9bcb4/32.png) [@jooma2](https://www.ruby-forum.com/u/jooma2)\
**Post date:** [August 26, 2010, 5:11pm UTC](https://www.ruby-forum.com/t/database-relationships-question/193922/2 "2010-08-26T17:11:16Z")

</div>

I think this is the relationship model?

character has\_and\_belongs\_to\_many quests  
character has\_many objectives, :through =\> quests

quest has\_and\_belongs\_to\_many characters  
quest has\_many objectives

---

<div class="post-metadata">

**Author:** ![jooma2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9bcb4/32.png) [@jooma2](https://www.ruby-forum.com/u/jooma2)\
**Post date:** [August 26, 2010, 6:26pm UTC](https://www.ruby-forum.com/t/database-relationships-question/193922/3 "2010-08-26T18:26:24Z")

</div>

TomRossi7 wrote:

> I think this is the relationship model?
> 
> character has\_and\_belongs\_to\_many quests  
> character has\_many objectives, :through =\> quests
> 
> quest has\_and\_belongs\_to\_many characters  
> quest has\_many objectives

I think I have to make a separate table. tracked\_objectives which  
belongs to character through quests.

As multiple people can do the same quests and will be at different steps  
at different times, they’ll need the same objectives but at different  
counts.

---

<div class="post-metadata">

**Author:** ![jooma2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9bcb4/32.png) [@jooma2](https://www.ruby-forum.com/u/jooma2)\
**Post date:** [August 27, 2010, 6:18pm UTC](https://www.ruby-forum.com/t/database-relationships-question/193922/4 "2010-08-27T18:18:43Z")

</div>

It sounds like you should avoid has\_and\_belongs\_to\_many since you are  
tracking attributes of the join relationship. Maybe something like  
this:

character has\_many objective\_assignments  
character has\_many objectives, :through =\> objective\_assignments  
character has\_many quest\_assignments  
character has\_many quests, :through =\> objective\_assignments

Does that seem right?
