I try to have a has_many through association on an belongs_to polymorphic association. For this I created, 3 class :
opinion.rb :
class Opinion < ActiveRecord::Base
has_many :opinion_types
has_many :name_applications, through: :opinion_types, source: :typeapplication, source_type: "NameApplication"
accepts_nested_attributes_for :opinion_types
end
opinion_type.rb :
class OpinionType < ActiveRecord::Base
belongs_to :opinion
belongs_to :typeapplication, polymorphic: true
accepts_nested_attributes_for :typeapplication
end
name_application.rb :
class NameApplication < ActiveRecord::Base
has_one :opinion_type, as: :typeapplication
has_one :opinion, through: :opinion_type
end
my OpinionsController :
class OpinionsController < ApplicationController
def new
@opinion = Opinion.new
3.times do
@opinion.name_applications << NameApplication.new
end
end
def create
@opinion = Opinion.new(params[opinions_params])
@opinion.save
redirect_to root_path
end
private
def opinions_params
params.require(:opinion).permit(opinion_types_attributes: [name_applications: [:name]])
end
end
my new.html.erb
<%= form_for @opinion do |o| %>
<%= o.fields_for :opinion_types do |ot| %>
<%= ot.fields_for :name_applications do |na| %>
<%= na.label :name, "Name" %><br />
<%= na.text_field :name %><br />
<% end %>
<% end %>
<%= o.submit %>
<% end %>
After submitting the form with 3 names, I receive:
{"utf8"=>"✓",
"authenticity_token"=>"blablabla",
"opinion"=>{"opinion_types_attributes"=>{"0"=>{"name_applications"=>{"name"=>"name test"}},
"1"=>{"name_applications"=>{"name"=>"name test 2"}},
"2"=>{"name_applications"=>{"name"=>"name test 3"}}}},
"commit"=>"Create Opinion"}
The problem is : I have a error unknown attribute: name_applications
When I write in Rails Console :
a = Opinion.new
a.name_applications << NameApplication.new(name: :"test")
a.name_applications << NameApplication.new(name: :"test 2")
a.name_applications << NameApplication.new(name: :"test 3")
a.save
My Opinion model and my NameApplication models associate to my Opinion model are correctly save.
I used different posts for try to find a solution but without succes :
- Rails 4 nested attributes and has_many :through associaton in a form
- Rails 3 submit a form with multiple records
- http://railscasts.com/episodes/196-nested-model-form-part-1
I don't know if my problem is :
- my
accepts_nested_attributes_for :typeapplication
in my opinion_type model (Because it a polymorphic association) - my strong parameters :
params.require(:opinion).permit(opinion_types_attributes: [name_applications: [:name]])
- or other
Thank you for your help
UPDATE : I find a solution but not very clean:
In my OpinionsController I changed my create action by this
def create
@opinion = Opinion.new
opinions_params[:opinion_types_attributes].each do |f, index|
@opinion.name_applications << NameApplication.new(opinions_params[:opinion_types_attributes][:"#{index}"][:name_applications])
end
@opinion.save
redirect_to root_path
end
I still do not understand why I can't just use @opinion = Opinion.new(opinions_params)
I will be grateful for any proposal that could help me to understand.