I’m using acts_as_taggable_on in an app and I would like to extract the tag ids (not the tag names) that a post has been tagged with.
My app has a posts controller, and in ruby console I can do:
>> post = Post.find(1)
=> #<Post id: 1, content: "Aliquam cupiditate ea deserunt et id placeat molest...", user_id: 1, created_at: "2011-07-06 19:29:44", updated_at: "2011-07-06 19:29:44">
>> tags = post.tag_counts_on("topics")
=> [#<ActsAsTaggableOn::Tag id: 1, name: "Politics">, #<ActsAsTaggableOn::Tag id: 2, name: "Economics">]
Here I have shown that the post is tagged with topic ids “Politics” and “Economics”. My problem is, I want to save this information on a cookie for use later. But I can’t store a hash on a cookie, I can only store strings of information. If I do:
session[:store_name] = tags.join(",")
And then later:
tags = session[:store_name].split(",")
I will get a hash:
["Politics", "Economics", ...]
But this hash doesn’t have a record of the tag_id for each topic tag. Is there any way to pull the ids out at some point and save them for later with the acts_as_taggable_on output? Or some suggestions on how to preserve the output from acts_as_taggable_on for later use?
If you need the id’s, just do