I have set the acts-as-taggable-on
gem in my model like this :
acts_as_taggable_on :deshanatags
It use the context deshanatags
. Now I need to get a list of all the tags (not only the ones assigned for one item. I need everything) in this context in the following format :
[
{"id":"856","name":"House"},
{"id":"1035","name":"Desperate Housewives"}
]
How can I do this?
I tried to follow many tutorials but hit dead ends because most of them are written for Rails 3. Rails for have some changes to the model like removal of attr_accessor which make it difficult for me to understand those tutorials. So please help.
Simply im trying to add Jquery Token input (http://loopj.com/jquery-tokeninput/) to my app
PS : Through Tagging table, is there a way to get a list of tags like the output of Tag.all by filtering the context?
The tags are stored in the Tags table, which you access from your program with e.g.
ActsAsTaggableOn::Tag.all
If you need more info on tag usage, there is also the table
ActsAsTaggableOn::Tagging
which contains links to where each tag is being used, including its context
To further apply this to your example, you can use
ActsAsTaggableOn::Tagging.includes(:tag).where(context: 'deshanatags').map { |tagging| { 'id' => tagging.tag_id.to_s, 'name' => tagging.tag.name } }.uniq
Let's explain the various parts in this statement:
To also cope with your additional problem, being taggins without tag, you could extend the where clause to
where("(context = 'deshanatags') AND (tag_id IS NOT NULL)")