user3704527
user3704527

Reputation: 23

Unable to create associated/ nested child record with "accepts_nested_attributes_for"

I get the following error on trying to create associated child record through parent using "accepts_nested_attributes_for":

ActiveRecord::RecordInvalid (Validation failed: Survey question details survey can't be blank, Survey question details activity detail must exist):

Wondering if I missing any piece for creation of child record automatically?

Sample input: (first 4 keys in the below input are merged into the params hash before saving)

{:organization_id=>"hardcoded", :activity_library_type=>0, :created_by=>"hardcoded", :updated_by=>"hardcoded", "activity_name"=>"s-s-28", "description"=>"", "content_path_url"=>"", "activity_status"=>0, "activity_type"=>4, "group"=>"group", "code_level"=>"code", "topic"=>"topic", "solution"=>"solution", "survey_question_details_attributes"=>[{"question"=>"Question1", "order"=>1, "question_type"=>0}]}

Parameters:

params.require(:activity_detail).permit(:activity_name, :description, :content_path_url, :activity_status,:activity_type,:group, :code_level, :topic, :solution, :duration_seconds, :survey_question_details_attributes => [:id, :question, :order, :question_type,:_destroy])

Below is the controller and the models (Each activity contains many question ):

class ActivityController < ApplicationController
def create
    # Merged additional_attributes hash to content_params recieved from front end (first 4 keys mentioned above in sample input)
    full_activity_details_record = additional_attributes.merge(content_params)
    response = ActivityDetail.create(full_activity_details_record)
  end
end

class ActivityDetail < ApplicationRecord
  has_many :survey_question_details, dependent: :destroy, foreign_key: :survey_id
  accepts_nested_attributes_for :survey_question_details

  validates :activity_name, :organization_id,
            :activity_library_type, :activity_status, :activity_type,
            :created_by, :updated_by, presence: true
  validates :activity_name, uniqueness: { case_sensitive: false }, length: { maximum: 55 }
  validates :description, length: { maximum: 160 }

end


class SurveyQuestionDetail < ApplicationRecord
  belongs_to :activity_detail, foreign_key: :survey_id

  validates :question, :survey_id, :question_type, :order, presence: true
  default_scope { order(:order) }
end

Upvotes: 0

Views: 216

Answers (2)

Dyaniyal Wilson
Dyaniyal Wilson

Reputation: 1060

I think the params created is incomplete. The params should come under :activity_detail key

:activity_detail => {
  {
    :organization_id=>"hardcoded", 
    :activity_library_type=>0, 
    :created_by=>"hardcoded", 
    :updated_by=>"hardcoded", 
    "activity_name"=>"s-s-28", 
    "description"=>"", 
    "content_path_url"=>"", 
    "activity_status"=>0, 
    "activity_type"=>4, 
    "group"=>"group", 
    "code_level"=>"code", 
    "topic"=>"topic", 
    "solution"=>"solution", 
    "survey_question_details_attributes"=>[
      {
        "question"=>"Question1", 
        "order"=>1, 
        "question_type"=>0
      }
    ]
  }
}

Upvotes: 0

Milind
Milind

Reputation: 5112

As you have not shared your views, Let me start the talking.

first of all, you have validated as presence true for the model attributes, hence you are getting validation error specifically.

####in SurveyQuestionDetail model
###this is causing the error => Survey question details SURVEY can't be blank
##you must either pass the survey_id as hidden field or comment it out
validates :question, :survey_id, :question_type, :order, presence: true

MOREOVER, in your view, you must use fields_for for nested attributes in your forms

something like

<%= form_with(@activity_detail: show, local: true) do |form| %>
  # Show name and label
  <%= form.fields_for :survey_question_detail do |s| %>
    <%= s.label :number %>
    <%= s.number_field :number %>
    ##can also include hidden fields to add other model...survey_id
  <% end %>
  # Submit Button
<% end %>

Hope this helps.

Upvotes: 0

Related Questions