开发者

Rails 3: Railscast #167 virtual attribute for creating tags - @user.tags

开发者 https://www.devze.com 2023-03-15 06:19 出处:网络
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

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 a开发者_如何学编程n help or insight into what I'm doing wrong.


@user.posts returns an array, so you could filter this pretty easily with something like this:

@user.posts.select do |post|
  post.tag_names.include? "music"
end

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:

Post.includes(:taggings => :tags).where("posts.user_id = ?", @user.id).select do |post|
  post.tag_names.include? "music"
end
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号