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.