Fetching people without a registration for a specific event
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开发者_StackOverflow中文版, 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?
In Ruby Controller:
@persons = Person.all(:order => "status ASC")
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 %>
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 "<<"
精彩评论