I hope my question is understandable, else I'm glad to clarify.
We have this sport event coming up, and I have the pleasure to register the people. :)
Facts:
- one can have a team of 5-10 persons.
- Need to know the order in which the people start.
- Need to know who the team leader is.
I did a form in the following style:
o [____Write first name here___]
<--- this is a textfieldo [____Write second name here__]
o [___________ ... ____________]
- ...
the "o" is a radio-button, used to pick the team leader.
I have a model Person and a model Team. Every team has a leader_id (which is one of the Person IDs). Furthermore every team :has_many
persons.
In the controller I have
def create
@team = Team.new(params[:team])
@team.save #just assume there are no errors
end
In the view I have (for the radio-buttons):
<input id="team_leader_id_**????**"
name="team[leader_id]"
type="radio"
value="**????**"
/>
<input id="team_leader_id_**????**"
name="team[leader_id]"
type="radio"
value="**????**"
/>
# etc.
My question: What should I put at **????**
?
I don't know the IDs of the persons yet, as they haven't been created. I have to put some meta-ID there, which ruby on rails recognizes and links everything correctly?
You don't have to read the rest (it describes the hack I'm using)
As said, at the moment I'm doing an ugly hack: first save the Team (without generating a leader), then fetch the same team from the db, get its people, and find the first person matching the value of
params[:team][:leader_id].
Finally saving this person's ID in the field of leader_id of the team.
But this code is inefficient, huge and buggy, that I suspect there is an easier way.