How to pass a Context when using acts_as_taggable_on for assigning tags

Posted by kbjerring on Stack Overflow See other posts from Stack Overflow or by kbjerring
Published on 2010-12-17T09:28:47Z Indexed on 2010/12/25 1:54 UTC
Read the original article Hit count: 538

I am using acts_as_taggable_on for assigning 'fixed' categories as well as free' tags to a Company model.

My question is how I can pass the correct tag Context for the two different kinds of tags in the form view. For instance, I would like the user to click all the "sector" categories that apply to a company, and then be freely allowed to add additional tags.

At this point, the only way I have gotten this to work is through the company model (inspired by this question):

# models/company.rb
class Company ...
  acts_as_taggable_on :sectors, :tags
  has_many :sector_tags, :through => :taggings, :source => :tag,
  has_many :taggings, :as => :taggable, :include => :tag, 
           :class_name => "ActsAsTaggableOn::Tagging",
           :conditions => { :taggable_type => "Company", :context => "sectors" }
...
end

in the form (using the simple_form gem) I have...

# views/companies/_form.html.haml
= simple_form_for @company do |f|
  = f.input :name
  = f.association :sector_tags, :as => :check_boxes, :hint => "Please click all that apply"
  = f.input :tag_list

But this obviously causes the two tag types ("sectors" and "tags") to be of the same "sectors" context which is not what I want.

Can anyone hint at how I can pass the relevant Context ("sectors") in the form where the user assigns the sector tags? Or maybe I can pass it in the "has_many :sector_tags ..." line in the Company model?

A related question is if this is a good way to do it at all? Would I be better off just using a Category model for assigning sector tags through a joint model?

Thanks!

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about associations