acts_as_taggable_on 输出什么样的哈希/数据,以及如何提取标签 ID 列表?
我在应用程序中使用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 的输出以供以后使用的一些建议?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您需要 id,只需这样做
If you need the id's, just do