0

I'm trying to build an app where there is a model for "Jobs" that can be associated with a "Tags" model where the associations are tracked using a join table. I'm wanting to find it it's possible to use a resource based form_for add checkboxes in the form to allow the user to choose which tags the job is associated with. The list of tags is set by the administrator, so they are not creating new tags but rather creating the associations. And I cant figure out how to do it. Most examples use a blog scenario where an article has_many comments and they are creating new comments and is very different from what I'm trying to do.

Models:

class Job < ActiveRecord::Base
    has_and_belongs_to_many :tags, :join_table => 'j_map_tags', :class_name => 'Tag', :foreign_key => 'job_id', :association_foreign_key => 'tag_id'
end

class Tag < ActiveRecord::Base
    has_and_belongs_to_many :jobs, :join_table => 'j_map_tags', :class_name => 'Job'
end

class JMapTag < ActiveRecord::Base
    belongs_to :job
    belongs_to :tag
end

Join Table Migration:

class CreateJMapTags < ActiveRecord::Migration
    def self.up
        create_table :j_map_tags, :id => false do |t|
            t.column    :job_id,    :integer
            t.column    :tag_id,    :integer
        end
    end

    def self.down
        drop_table :j_map_tags
    end
end

Routes:

resources :jobs, :module => 'manager', :constraints => lambda { |request| request.xhr? } do
    resources :tags
end

ERB:

<%= form_for [Job.new], :remote => true do |form| %>
    ...
    <% form.label  ???????? %>
    <% form.check_box  ???????? %>
    ...
<% end %>

Is this even possible via resource based form_for? I haven't been able to find any examples involving this type of resource relationship.

Reuben
  • 53
  • 5

1 Answers1

0

I think this is what you need: HABTM Checkboxes

Mirko
  • 5,207
  • 2
  • 37
  • 33
  • Thanks, that makes more sense. I was getting lost looking for a way to do form.check_box rather than check_box_tag and was confused as to how to iterate over the set of tags when there was no relationship established between the two yet. Thaks for the link! – Reuben Sep 03 '11 at 20:38
  • No problem. If this answers you question, mark it as a correct answer. – Mirko Sep 04 '11 at 11:12
  • For those w/ a Railscasts subscription, there's a revised episode for HABTM checkboxes. http://railscasts.com/episodes/17-habtm-checkboxes-revised – Jack Frost Jul 17 '12 at 19:26