Hi Marnen,
Thanks for taking an interest. I really appreciate you and Rick taking
the time to check on my problem.
I finally made the whole thing work (kind of). I know I am doing
something wrong because the associated objects validations are not
being hit, probably because I am creating the wrong type of object,
('project' rather than 'person_project') for the form as you can see
in the params hash below, but at this point I am already too tired of
the problem and I am also too late to keep looking into it or I won't
meet the deadline I have. I would like to fix it in the (near) future,
though, so here goes some more info in case it helps.
This is a sample of the params hash (edited):
person: !map:HashWithIndifferentAccess
# person fields and values go here
person_projects_attributes: !map:HashWithIndifferentAccess
"0": !map:HashWithIndifferentAccess
project_attributes: !map:HashWithIndifferentAccess
id: "000267"
project_id: "000267"
supervisor_id: "10"
id: "10326"
"1": !map:HashWithIndifferentAccess
project_attributes: !map:HashWithIndifferentAccess
id: 000392
project_id: 000392
supervisor_id: "2"
I ended up validating the associated objects values by hand in the
controller. Something like this:
def validate_person_projects_list
params[:person][:person_projects_attributes].each_pair do |index,
person_project|
if person_project[:supervisor_id].blank?
@person.errors.add_to_base 'All projects must have a
supervisor.'
return
end
end
end # def validate_person_projects
This is the form (edited):
<% form_for :person, @person, :url => { :action => "approval" } do |f|
%>
# person's fields go here
<% for @person_project in @person.person_projects %>
<% f.fields_for :person_projects, @person_project do |
pp_fields| %>
<%= pp_fields.hidden_field :project_id -%>
<%= pp_fields.select :supervisor_id,
@person_project.supervisors.map {|s| [s.id.to_s + ' ->
' + s.lname.strip + ', ' + s.fname.strip, s.id]}.sort {|a, b| a[0] <=>
b[0]} -%>
<% end %>
<% end %>
<%= submit_tag 'Save' -%>
<% end -%>
@person_project#supervisors is a model method that will return the
list of supervisor objects to choose from: current record's, current
record project's and current record project contractors'.
Thanks again for taking the time. I really appreciate it.