0

Huston, we have a problem:

class FirstModel 
 has_many :merged_models
 has_many :second_models, :through => :merged_models
end

class SecondModel 
 has_many :merged_models
 has_many :first_models, :through => :merged_models
end

class MergedModel 
 belongs_to :first_model
 belongs_to :second_model
end

Form:

<%= form_for(first_model) do |f| %>
 <%= f.fields_for :merged_model do |ff| %>
    <%= ff.label :date %>
    <%= ff.date_select :start_date %>

Problem:

Processing by FirstModelsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"f+D8AaVzM6ahrUyo/nwxISFEleVrXGxo8m30sIiLIe7gvG8J9KfONjuT09j6z3M4Rvw+n3Hm6PMddOtfbgjt5g==", "first_model"=>{"first_name"=>"yyyy", "last_name"=>"yyy", "merged_model"=>{"start_date(1i)"=>"2017", "start_date(2i)"=>"2", "start_date(3i)"=>"28", "second_model_id"=>"1"}}, "commit"=>"Create"} Unpermitted parameter: merged_model Unpermitted parameter: merged_model

First model's controller's strong params:

  params.require(:first_model).permit(:first_name, :last_name, merged_models_attributes: [:id, :start_date])

First model acccepts nested attributes of merged model:

  accepts_nested_attributes_for :merged_models

However, after creating a FirstModel, MergedModel does not get created. Tried to create it in form:

  <%= f.fields_for :merged_model [first_model.merged_models.build] do |ff| %>

But got:

no implicit conversion of MergedModel into Integer

Not completely understand what that means..

Also tried creating a new MergedModel from a FirstModel's create action, with a bang:

@merge_model = MergedModel.create!

And got the same error - no implicit conversion...

Could anyone explain more about this? I feel its about passing an Array of my MergedModel's params into MergedModel's params...? I am totally lost here...

2
  • 1
    Your form should have fields_for :merged_models instead of just merged_model Commented Feb 28, 2017 at 16:31
  • I'm glad it worked. We've all been there. Commented Feb 28, 2017 at 19:50

2 Answers 2

1

Your form should have fields_for :merged_models instead of just merged_model.

Sign up to request clarification or add additional context in comments.

Comments

1

On fresh installs of Rails5 applications, belongs_to implies optional: false by default (previously called required: true).

You need to create a MergedModel both with FirstModel AND SecondModel associated...

It looks like you are trying create a MergedModel only with a FirstModel associated, if SecondModel is optional, you need to say that for belongs_to with...

belongs_to :second_model, optional: true

1 Comment

sorry pal, I forgot to rewrite that I already added that to MergedModel (not because I knew about this change in Rails5, but because I really needed that optional:true functionality)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.