I have the following User model:
class User < ActiveRecord::Base
has_many :competences
has_many :skills, :through => :competences
accepts_nested_attributes_for :skills
end
and the following Skill model:
class Skill < ActiveRecord::Base
has_many :competences
has_many :users, :through => :competences
end
The Competence model has a ‘type’ attribute and it is the join model. Inside the nested form, How can I set the ‘type’ attribute while I save the submitted skills?
Here is the nested form:
<% f.fields_for :skills do |s| %>
<%= s.text_field :name %>
<% end %>
You need to create the Competence object specifically — you can’t implicitly create it (a la HABTM) and also set attributes on it.
Your User model should accept nested attributes for Competences, which should accept nested attributes for Skills. Something like this should put you on the right track: