Reputation: 758
As in subject - how to do it ? I did some research over the internet but all solutions i found was either not working or unclear for me. Maybe i'm just miserable searcher. Anyway, this is the case:
I have two tables: events and participants. All field are pretty standard for purpose, but i have also this less-obvious fields: 'captaken' in event and 'amount' in participant. Basic idea is that every participant specify number of people to sign up for event (eg. smbdy will come with wife or gf, so 'amout' is 2). And every event has limited number of participant - current amount of taken seats is stored in 'captaken' field.
I have simple scaffolded form for adding participant, and the goal is to make 'captaken' field of specify event increment according to 'amount' each time new participant is signing up (creating).
How could i make it happen ?
Thanks in advance.
Upvotes: 2
Views: 763
Reputation: 496
Here's the simplest possible way to do it in your participant model:
#Specify the relationship, model should have an event_id field.
belongs_to :event
#This is the hook to tell the system to update the event
#after creation.
after_create :update_event_captaken
#Method for updating the event
def update_event_captaken
self.event.captaken += self.amount
self.event.save
end
Upvotes: 2
Reputation: 332
Use after_create callback in participant model. which will work as Whenever new record is created in participants update the value In another table.
Upvotes: 1