# Difficult query

**URL:** <https://www.ruby-forum.com/t/difficult-query/123865>\
**Category:** Rails\
**Created:** [December 21, 2007, 3:05pm UTC](https://www.ruby-forum.com/t/difficult-query/123865 "2007-12-21T15:05:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tarscher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@Tarscher](https://www.ruby-forum.com/u/Tarscher)\
**Post date:** [December 21, 2007, 3:05pm UTC](https://www.ruby-forum.com/t/difficult-query/123865/1 "2007-12-21T15:05:47Z")

</div>

Hi all,

I’m having problems creating a query.

I have an event that has many attendees  
events\_table: id, name  
attendees\_table: id, user\_id, event\_id, present

I want to create a query that shows all the events to a user and if he  
will be present (the user can add/change his presens). If he hasn’t  
yet added his presense than there is no record in the attendees table  
ofcourse

An example of a queryresult can look like:  
events.id | attendees.user\_id | attendees.present  
1 2 1 \<–  
will be present at event 1  
2 2 null \<–  
has not yet stated his presense at event 2  
3 2 0 \<–  
will not be present at event 3

Someone has an idea?

Thanks,  
Stijn

---

<div class="post-metadata">

**Author:** ![Tarscher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@Tarscher](https://www.ruby-forum.com/u/Tarscher)\
**Post date:** [December 21, 2007, 3:28pm UTC](https://www.ruby-forum.com/t/difficult-query/123865/2 "2007-12-21T15:28:40Z")

</div>

Tarscher wrote:

> Hi all,
> 
> I’m having problems creating a query.
> 
> I have an event that has many attendees  
> events\_table: id, name  
> attendees\_table: id, user\_id, event\_id, present
> 
> I want to create a query that shows all the events to a user and if he  
> will be present (the user can add/change his presens). If he hasn’t  
> yet added his presense than there is no record in the attendees table  
> ofcourse
> 
> An example of a queryresult can look like:  
> events.id | attendees.user\_id | attendees.present  
> 1 2 1 \<–  
> will be present at event 1  
> 2 2 null \<–  
> has not yet stated his presense at event 2  
> 3 2 0 \<–  
> will not be present at event 3
> 
> Someone has an idea?
> 
> Thanks,  
> Stijn

this should look a bit like this

user model:  
has\_many :attendees  
has\_many :events, :through =\> :attendees

attendee model:  
belongs\_to :user  
belongs\_to :event

event model:  
has\_many :attendees

then a simple  
@user.events  
should return all records for which the user has an attendees record  
and @user.attendees[0].present gives his presence  
eg in view:

 \<% @user.attendees.each do |attend| %\> - \<%= attend.event.name %\>\<% "is present" if attend.present %\>
 \<% end %\>

hope i didn’t miss an important point

---

<div class="post-metadata">

**Author:** ![Tarscher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@Tarscher](https://www.ruby-forum.com/u/Tarscher)\
**Post date:** [December 21, 2007, 4:00pm UTC](https://www.ruby-forum.com/t/difficult-query/123865/3 "2007-12-21T16:00:52Z")

</div>

You are right. That is much better.

1 question thuogh:  
maybe attendee is not a good name because calling it attendee and also  
using it for not attending the event is a bit weird I think.

I’m looking for a suitable word but my English fails me. Maybe you  
have a suggestion?

Thanks  
Stijn

On 21 dec, 15:28, Thorsten M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Tarscher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@Tarscher](https://www.ruby-forum.com/u/Tarscher)\
**Post date:** [December 21, 2007, 4:22pm UTC](https://www.ruby-forum.com/t/difficult-query/123865/4 "2007-12-21T16:22:07Z")

</div>

participant  
subscriber

would be alternates to attendee

but i would use ‘registrations’ or ‘enlists’, since that’s better  
describing that it links an user with an event
