Rails 3:Railscast #167 用于创建标签的虚拟属性 - @user.tags
我想要一个标记系统,可以根据创建标签的用户将标签分开。我遵循 Railscast #167 关于使用虚拟属性设置标签的方法,但这种方式只能让我调用 @post.tags 来查找帖子的标签,但我无法调用 @user.tags 并找到它们的所有标签。
我如何增强这一点,以便 @user.posts.tag("music") 返回带有音乐标签的所有帖子?
感谢您对我做错了什么的帮助或见解。
I would like to a tagging system where I can separate the tags by the user's who created them. I followed Railscast #167 about setting up tags using virtual attributes, but that way only lets me call @post.tags to find the tags for a post, but I can't call @user.tags and find all their tags.
How could I augment this so that @user.posts.tag("music") would return all their posts with the tag music?
Thanks for an help or insight into what I'm doing wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
@user.posts
返回一个数组,因此您可以使用以下内容轻松过滤它:但是,您可能会遇到在这种情况下未急切加载记录的问题。像这样的事情应该解决这个问题:
@user.posts
returns an array, so you could filter this pretty easily with something like this:You might, however, run into an issue with the records not being eagerly loaded in that situation. Something like this should take care of that: