# Relationships problems to access

**URL:** <https://www.ruby-forum.com/t/relationships-problems-to-access/109310>\
**Category:** Rails\
**Created:** [July 13, 2007, 11:22am UTC](https://www.ruby-forum.com/t/relationships-problems-to-access/109310 "2007-07-13T11:22:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![fcano](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e19adc/32.png) [@fcano](https://www.ruby-forum.com/u/fcano)\
**Post date:** [July 13, 2007, 11:22am UTC](https://www.ruby-forum.com/t/relationships-problems-to-access/109310/1 "2007-07-13T11:22:20Z")

</div>

Hi!  
I have an events table, a groups table and invitations table. Invitation  
has event\_id and group\_id. So, Invitation belongs\_to event and  
belongs\_to group.  
I have a user table too. User has\_many memberships and a membership  
belongs\_to user and belongs\_to group.  
So, A user is member of some groups that are invited to events. I want  
to know the events that the groups of a user is invited to.  
Something like @user.groups.events, but this throws and exception.  
Thanks!

---

<div class="post-metadata">

**Author:** ![fcano](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e19adc/32.png) [@fcano](https://www.ruby-forum.com/u/fcano)\
**Post date:** [July 13, 2007, 12:28pm UTC](https://www.ruby-forum.com/t/relationships-problems-to-access/109310/2 "2007-07-13T12:28:46Z")

</div>

In User  
has\_many :groups, :through=\>memberships

But use it like this user.groups[0].events. Remember groups is a list.

Khurram Ijaz.  
[http://www.renai-soft.com](http://www.renai-soft.com)

Florencio C. wrote:

> Hi!  
> I have an events table, a groups table and invitations table. Invitation  
> has event\_id and group\_id. So, Invitation belongs\_to event and  
> belongs\_to group.  
> I have a user table too. User has\_many memberships and a membership  
> belongs\_to user and belongs\_to group.  
> So, A user is member of some groups that are invited to events. I want  
> to know the events that the groups of a user is invited to.  
> Something like @user.groups.events, but this throws and exception.  
> Thanks!

---

<div class="post-metadata">

**Author:** ![fcano](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e19adc/32.png) [@fcano](https://www.ruby-forum.com/u/fcano)\
**Post date:** [July 13, 2007, 2:00pm UTC](https://www.ruby-forum.com/t/relationships-problems-to-access/109310/3 "2007-07-13T14:00:17Z")

</div>

I think there isn’t a nice solution at this point,  
what you want is;

has\_many :events, :through =\> {:memberships =\> :groups}

or something,  
but I don’t think that works currently,

as a slightly streamlined version of your current method;

def next\_events  
self.groups.map(&:events).flatten  
end

but that may not be efficient, unless you find your user with  
User.find(id, :include =\> {:groups =\> :events})

alternatively

def next\_events  
Events.find(:all,  
:include =\> {:group =\> :user},  
:conditions =\> [“users.id = ?”, self.id]  
)  
end

but that wont be cached.

personally I’d make a custom scoping

def events\_scope  
subquery = \<\<\_SQL  
SELECT \* FROM invitations  
INNER JOIN groups ON invitations.group\_id = groups.id

```
    WHERE
        invitations.event_id = events.id
            AND
        EXISTS (
            SELECT * FROM memberships
                INNER JOIN users ON memberships.group_id = groups.id
            WHERE membership.user_id = ?
            )

```

\_SQL  
Event.with\_scope(  
:find =\> {  
:conditions =\> [subquery, self.id]  
}) do  
return yield  
end  
end

def all\_events  
events\_scope do  
Event.find(:all)  
end  
end

but I’m mental in love with with\_scope, I guess.

MatthewRudy

Florencio C. wrote:

> Hello!  
> And what if I want to collect all the events from all the groups related  
> to a user?  
> I’m using this solution that does not seem very neat:
> 
> def next\_events  
> next\_events = []  
> for group in self.groups  
> for event in group.events  
> next\_events \<\< event  
> end  
> end  
> end

---

<div class="post-metadata">

**Author:** ![fcano](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e19adc/32.png) [@fcano](https://www.ruby-forum.com/u/fcano)\
**Post date:** [July 13, 2007, 12:33pm UTC](https://www.ruby-forum.com/t/relationships-problems-to-access/109310/4 "2007-07-13T12:33:58Z")

</div>

Hello!  
And what if I want to collect all the events from all the groups related  
to a user?  
I’m using this solution that does not seem very neat:

def next\_events  
next\_events = []  
for group in self.groups  
for event in group.events  
next\_events \<\< event  
end  
end  
end

Khurram Ijaz wrote:

> In User  
> has\_many :groups, :through=\>memberships
> 
> But use it like this user.groups[0].events. Remember groups is a list.
> 
> Florencio C. wrote:
> 
> > Hi!  
> > I have an events table, a groups table and invitations table. Invitation  
> > has event\_id and group\_id. So, Invitation belongs\_to event and  
> > belongs\_to group.  
> > I have a user table too. User has\_many memberships and a membership  
> > belongs\_to user and belongs\_to group.  
> > So, A user is member of some groups that are invited to events. I want  
> > to know the events that the groups of a user is invited to.  
> > Something like @user.groups.events, but this throws and exception.  
> > Thanks!

---

<div class="post-metadata">

**Author:** ![fcano](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e19adc/32.png) [@fcano](https://www.ruby-forum.com/u/fcano)\
**Post date:** [July 13, 2007, 3:26pm UTC](https://www.ruby-forum.com/t/relationships-problems-to-access/109310/5 "2007-07-13T15:26:33Z")

</div>

Think this plugin does what you want:  
[http://agilewebdevelopment.com/plugins/nested\_has\_many\_through](http://agilewebdevelopment.com/plugins/nested_has_many_through)

seems it might also become part of the rails core in the future…

On 13 Jul., 14:02, Matthew R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![fcano](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e19adc/32.png) [@fcano](https://www.ruby-forum.com/u/fcano)\
**Post date:** [July 13, 2007, 2:02pm UTC](https://www.ruby-forum.com/t/relationships-problems-to-access/109310/6 "2007-07-13T14:02:10Z")

</div>

A correction to my scoping,

Event.with\_scope(:find =\> {  
:conditions =\> [“EXISTS (#{subquery})”, self.id]  
}) do  
return yield  
end

> def events\_scope  
> subquery = \<\<\_SQL  
> SELECT \* FROM invitations  
> INNER JOIN groups ON invitations.group\_id = groups.id
> 
> ```
> WHERE
> invitations.event_id = events.id
> AND
> EXISTS (
> SELECT * FROM memberships
> INNER JOIN users ON memberships.group_id = groups.id
> WHERE membership.user_id = ?
> )
> 
> ```
> 
> \_SQL  
> Event.with\_scope(  
> :find =\> {  
> :conditions =\> [subquery, self.id]  
> }) do  
> return yield  
> end  
> end
