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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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, > Stijnthis 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: <ul> <% @user.attendees.each do |attend| %> <li><%= attend.event.name %><% "is present" if attend.present %></li> <% end %> </ul> hope i didn''t miss an important point -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Mueller <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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: > > <ul> > <% @user.attendees.each do |attend| %> > <li><%= attend.event.name %><% "is present" if attend.present > %></li> > <% end %> > </ul> > > hope i didn''t miss an important point > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---