Jonathan Apostoles
2005-Jul-12 01:56 UTC
Is there an easier way to do this? (ActiveRecord / has_many from form post)
** Sorry for reposting this.. I accidentally replied to an unrelated post to ** post this. My sincere apologies. I have a "has_many" association that populates a form from a few different tables that is saved in an Inspection object. Here''s the create action in the controller to populate inspection.inspection_results: def create @inspection = Inspection.new( @params[:inspection] ) @inspection.date = Time.now @inspection_results = @params[:inspection_results]; @inspection_results.each_value do |curResult| newResult = InspectionResult.new newResult.inspection_point_id = curResult["inspection_point_id"] newResult.inspection_flag_id = curResult["inspection_flag_id"] @inspection.inspection_results << newResult end @inspection.save end So I have values posted to the controller as an InspectionResult object. My question is, is there a way (through correct naming of form elements, or other method) to not have to manually do this? I tried naming the elements with names like "inspection[inspection_results[0][inspection_point_id]]" but that threw errors. I''m sure there''s a way to have this "has_many" association in an ActiveRecord get populated from a form post the same way the rest of the object is auto-populated. Any ideas? Thanks, Jonathan Apostoles