# Database question

**URL:** <https://www.ruby-forum.com/t/database-question/162071>\
**Category:** Rails\
**Created:** [March 5, 2009, 2:19pm UTC](https://www.ruby-forum.com/t/database-question/162071 "2009-03-05T14:19:42Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![pesho318i](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ec9cab/32.png) [@pesho318i](https://www.ruby-forum.com/u/pesho318i)\
**Post date:** [March 5, 2009, 2:19pm UTC](https://www.ruby-forum.com/t/database-question/162071/1 "2009-03-05T14:19:42Z")

</div>

## hi, I have to settle a database query problem, and I am a bit confused. I have 4 tables:

Members  
Groups  
Roles  
Memberships: member\_id, group\_id, role\_id  
Messages: concerned\_group\_id, content

If a member is logged in the application, I would like to display the  
messages, regarding the groups (if any) of which the member is admin.  
(admin is a role)

I don’t know what is the best way to do that, but:  
First I can find all Memberships, where the member is admin:  
admin\_role\_id = Role.find\_by\_name(“admin”).id  
Membership.find(:all, :conditions =\> [‘member\_id = ? and role\_id = ?’,  
member.id, admin\_role\_id])

Each from the found Memberships has a group\_id  
Then how can I find all the Messages, that concern the groups with these  
group\_id-s?

Thank you!

---

<div class="post-metadata">

**Author:** ![pesho318i](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ec9cab/32.png) [@pesho318i](https://www.ruby-forum.com/u/pesho318i)\
**Post date:** [March 5, 2009, 4:36pm UTC](https://www.ruby-forum.com/t/database-question/162071/2 "2009-03-05T16:36:39Z")

</div>

That appears to be 5 tables, but I presume that is just a typo. What  
are  
the has and belongs to relationships?

2009/3/5 Pesho P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![pesho318i](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ec9cab/32.png) [@pesho318i](https://www.ruby-forum.com/u/pesho318i)\
**Post date:** [March 5, 2009, 9:19pm UTC](https://www.ruby-forum.com/t/database-question/162071/3 "2009-03-05T21:19:40Z")

</div>

2009/3/5 Pesho P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> has\_many :memberships, :dependent =\> :destroy  
> has\_many :memberships, :dependent =\> :destroy  
> belongs\_to :member  
> belongs\_to :role  
> belongs\_to :group

Are you sure? That seems incredibly complex. For example a member has  
many  
roles, but it also has many memberships, each one of which belongs to a  
role, so that is another set of roles a member may have.

Does a message belong to a member, a group and a role or just one of  
them at  
a time?

---

<div class="post-metadata">

**Author:** ![pesho318i](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ec9cab/32.png) [@pesho318i](https://www.ruby-forum.com/u/pesho318i)\
**Post date:** [March 5, 2009, 5:59pm UTC](https://www.ruby-forum.com/t/database-question/162071/4 "2009-03-05T17:59:52Z")

</div>

Colin L. wrote:

> That appears to be 5 tables, but I presume that is just a typo.

yes, sorry for the typo.

> What are the has and belongs to relationships?

## Member:

has\_many :memberships, :dependent =\> :destroy  
has\_many :roles, :dependent =\> :destroy  
has\_many :messages, :dependent =\> :destroy

## Group:

has\_many :mberships,:dependent =\> :destroy  
has\_many :messages, :dependent =\> :destroy

## Role:

has\_many :memberships, :dependent =\> :destroy  
has\_many :messages, :dependent =\> :destroy

## Membership:

belongs\_to :member  
belongs\_to :group  
belongs\_to :role

## Message:

belongs\_to :member  
belongs\_to :role  
belongs\_to :group

🙂

---

<div class="post-metadata">

**Author:** ![pesho318i](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ec9cab/32.png) [@pesho318i](https://www.ruby-forum.com/u/pesho318i)\
**Post date:** [March 5, 2009, 11:09pm UTC](https://www.ruby-forum.com/t/database-question/162071/5 "2009-03-05T23:09:46Z")

</div>

Am I right in thinking that the member/group combination for a message  
is  
always the same member/group that appears in a membership record? If so  
then would it be better to have

Membership:  
has\_many :messages  
belongs\_to :member  
belongs\_to :group  
belongs\_to :role

and

Message:  
belongs\_to :membership

Then you don’t need the has\_many :messages for Member or Group and I  
think  
the whole thing is simpler.

2009/3/5 Pesho P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![pesho318i](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ec9cab/32.png) [@pesho318i](https://www.ruby-forum.com/u/pesho318i)\
**Post date:** [March 5, 2009, 10:47pm UTC](https://www.ruby-forum.com/t/database-question/162071/6 "2009-03-05T22:47:42Z")

</div>

Colin L. wrote:

> Are you sure? That seems incredibly complex. For example a member has  
> many  
> roles, but it also has many memberships, each one of which belongs to a  
> role, so that is another set of roles a member may have.
> 
> Does a message belong to a member, a group and a role or just one of  
> them at  
> a time?

ok Colin, you’re absolutely right! I’m afraid I’ve totally messed up my  
dependencies. This is my first time to handle database relationships  
through Rails models.

So, I believe this is what I want to have:

Member:  
has\_many :memberships, :dependent =\> :destroy  
has\_many :messages, :dependent =\> :destroy

Group:  
has\_many :memberships, :dependent =\> :destroy  
has\_many :messages, :dependent =\> :destroy

Role:  
has\_many :memberships, :dependent =\> :destroy

Membership:  
belongs\_to :member  
belongs\_to :group  
belongs\_to :role

Message:  
belongs\_to :member  
belongs\_to :group

A message is supposed to be written by a member, who is a part of group.  
So I believe a message should belong to a member and a group.  
But different members from the same group can write different messages.  
And also, the same member, who is a part of different groups can write  
different messages on behalf of the different groups he/she is a part  
of.

I hope the relationships above reflect well my expectations that I just  
described.

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![pesho318i](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ec9cab/32.png) [@pesho318i](https://www.ruby-forum.com/u/pesho318i)\
**Post date:** [March 6, 2009, 9:42am UTC](https://www.ruby-forum.com/t/database-question/162071/7 "2009-03-06T09:42:50Z")

</div>

2009/3/5 Pesho P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> > belongs\_to :group

Yes. The question is, of course, has any of this helped with your  
original  
question? I think it has as you can now find all messages where the  
membership.role\_id is admin and membership.member\_id is the member you  
want

---

<div class="post-metadata">

**Author:** ![pesho318i](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ec9cab/32.png) [@pesho318i](https://www.ruby-forum.com/u/pesho318i)\
**Post date:** [March 5, 2009, 11:33pm UTC](https://www.ruby-forum.com/t/database-question/162071/8 "2009-03-05T23:33:48Z")

</div>

Colin L. wrote:

> Am I right in thinking that the member/group combination for a message  
> is  
> always the same member/group that appears in a membership record? If so  
> then would it be better to have
> 
> Membership:  
> has\_many :messages  
> belongs\_to :member  
> belongs\_to :group  
> belongs\_to :role
> 
> and
> 
> Message:  
> belongs\_to :membership

you are right. Then should I remove the member\_id and group\_id fields  
from table Messages, and substitute them with a field membership\_id?

🙂

---

<div class="post-metadata">

**Author:** ![pesho318i](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ec9cab/32.png) [@pesho318i](https://www.ruby-forum.com/u/pesho318i)\
**Post date:** [March 6, 2009, 11:25am UTC](https://www.ruby-forum.com/t/database-question/162071/9 "2009-03-06T11:25:08Z")

</div>

2009/3/6 Pesho P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> you are right. This is the logic, but how do I express it in Rails  
> syntax??  
> (I think) I cannot use Message.find, because the Message model doesn’t  
> have info about roles…

Yes it does, the relationships give it that information. If you have a  
message then you can say a\_message.membership.role. You can also do  
something like  
Message.find(:all, :include =\> :membership, :conditions =\>  
[‘membership.role\_id =?’, a\_role\_id])  
I am not guaranteeing the exact syntax, I may have made a silly mistake  
as I  
have not done much yet myself. Possibly a bit of a case of the blind  
leading the blind. Play about a bit and look in development.log to see  
the  
actual sql if it is not working for you.

---

<div class="post-metadata">

**Author:** ![pesho318i](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ec9cab/32.png) [@pesho318i](https://www.ruby-forum.com/u/pesho318i)\
**Post date:** [March 6, 2009, 11:00am UTC](https://www.ruby-forum.com/t/database-question/162071/10 "2009-03-06T11:00:26Z")

</div>

Colin L. wrote:

> Yes. The question is, of course, has any of this helped with your  
> original  
> question? I think it has as you can now find all messages where the  
> membership.role\_id is admin and membership.member\_id is the member you  
> want

yes, we are back to the first question 🙂  
you are right. This is the logic, but how do I express it in Rails  
syntax??  
(I think) I cannot use Message.find, because the Message model doesn’t  
have info about roles…

Best,  
Petar

---

<div class="post-metadata">

**Author:** ![pesho318i](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ec9cab/32.png) [@pesho318i](https://www.ruby-forum.com/u/pesho318i)\
**Post date:** [March 6, 2009, 11:31am UTC](https://www.ruby-forum.com/t/database-question/162071/11 "2009-03-06T11:31:31Z")

</div>

lol, that’s great!

THANKS A LOT, Colin ! ! !

Have a great weekend 🙂  
Petar
