views:

25

answers:

1

Hi, 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 %>
A: 

You need to create the Competence object specifically -- you can't implicitly create it (a la HABTM) and also set attributes on it.

@user.competences.create(:type => 'something', :skill => @skill)

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:

<% f.fields_for :competences do |c| %>
  <%= c.text_field :type %>
  <% c.fields_for :skills do |s| %>
    <%= s.text_field :name %>
  <% end %>
<% end %>
Dave Pirotte