0

I have three models, and i am trying to save onto a third table using simple form gem and checkboxes.

class Work < ActiveRecord::Base
    has_many :skills, through: :skillships
end

The second model is

class Skill < ActiveRecord::Base
   has_many :works, through: :skillships
end

The third is

class Skillship < ActiveRecord::Base
  belongs_to :work
  belongs_to :skill
end

Using the Work model i am trying to save the data on the skillship table. Something similar to this http://railscasts.com/episodes/17-habtm-checkboxes-revised. Can you please help.

EDIT

The form

<%= simple_form_for(@work) do |f| %>
  <%= f.error_notification %>

<div class="form-inputs">

    <%= f.input :title, :label => 'Project Title' %>
    <%= f.input :excerpt, :as => :text %>

   <fieldset>
   <legend>Skills Used </legend> 
      Would like to check the skills i used here.
   </fieldset>


  </div>

  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>

I tried..

<%= hidden_field_tag "work[skill_ids][]", nil %>
<% Skill.all.each do |skill| %>
  <%= check_box_tag "work[skill_ids][]", skill.id, @work.skill_ids.include?(skill.id), id: dom_id(skill) %>
  <%= label_tag dom_id(skill), skill.title %><br>
<% end %>

The reason i'm doing this it because work can have many skills used.

Benjamin
  • 2,108
  • 2
  • 26
  • 46

1 Answers1

1

I was going about this the wrong way. A join table solved the problem.

rails g migration create_skills_works_table

Then

class CreateSkillsWorksTable < ActiveRecord::Migration
  def self.up
    create_table :skills_works, :id => false do |t|
        t.references :skill
        t.references :work
   end

    add_index :skills_works, [:skill_id, :work_id]
    add_index :skills_works, [:work_id, :skill_id]

  end

  def self.down
    drop_table :skills_works
  end

end

Using simple form on the work view.

<fieldset>
   <legend>Skills Used </legend> 
        <%= f.association :skills %>
</fieldset>
Benjamin
  • 2,108
  • 2
  • 26
  • 46