views:

38

answers:

3

I'm trying to fetch all people without a registration for a specific event.

My models are Person, Event and Registration.

In the MySQL table it would add an entry to the registrations table with the person.id and event.id (plus comment and a "status" - 1 for registration, 2 for deregistration).

Now, I'm trying to get all people which did no action, all registrations and all deregistrations (in that order). How can I do that?

edit: may I add some more information:

Structure in database:
people: id, name, mail ...
events: id, title, date ...
registrations: id, person_id, event_id, status ...

Problem:
It's easy to select all registrations and deregistrations. But I've also to select all people which did no action for an event.

One way I thought about is to select all people from the people model. Then select all registrations from the specific event, and finally compare the two arrays. What do you think?

A: 

In Ruby Controller:

@persons = Person.all(:order => "status ASC")
Trip
This won't work because status is in the model "registrations" and the registration is from a specific event
andi
A: 

If you want to sort it out in your view you can also use the #select method on all the Person objects:

#controller
@people = Person.all

#view
<h2> registered people </h2>
<% @people.select { |person| person.status == 1 }.each do |reg| %>
....
<% end %>

<h2> de-registered people </h2>
<% @people.select { |person| person.status == 2 }.each do |reg| %>
....
<% end %>
Bryce
I think that wouldn't work, because a person has no or one registration per event. So there are multiple events where a person can register or de-register. If someone registers, it would add a registration for this event with "status=1"
andi
ok, just shooting from the hip without seeing your code or tests. Instead, of @people = Person.all, I think you could substitute with @people = Event.find(1).people and it would grab just the people for that single event
Bryce
A: 

I did the following:

Select all People, then loop through all events and select all people which registered or deregistered. Then subtract them from all people...

That's it.


#model
def all_register_people
    registrations = Registration.find_all_by_event_id self.id
    people = []

    for registration in registrations
      people "shift" registration.person
    end

    people
  end

#view
all_people = Person.all(:order => "firstname")

...

register_event_people = event.all_register_people
people_no_action = all_people - register_event_people

people_reg = Person.all(...)
people = people_no_action | people_reg

"shift" means "<<"

andi