我在应用程序中使用acts_as_taggable_on,我想提取已标记帖子的标签ID(不是标签名称)。
我的应用程序有一个帖子控制器,在 ruby 控制台中我可以这样做:
>> 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">]
在这里,我展示了该帖子的主题 ID 为“政治”和“经济”。我的问题是,我想将此信息保存在 cookie 中以备后用。但是我不能在 cookie 上存储哈希值,我只能存储信息字符串。如果我做:
session[:store_name] = tags.join(",")
然后后来:
tags = session[:store_name].split(",")
我会得到一个哈希:
["Politics", "Economics", ...]
但是这个哈希没有每个主题标签的 tag_id 记录。有什么方法可以在某个时候提取 id 并使用acts_as_taggable_on 输出保存它们以供以后使用?或者关于如何保留acts_as_taggable_on 的输出以供以后使用的一些建议?