【问题标题】:Rails 4: Using acts_as_taggable_on to link two models which have common tagsRails 4:使用acts_as_taggable_on 链接两个具有公共标签的模型
【发布时间】:2015-04-13 13:49:39
【问题描述】:

我有 2 个模型。一个用户模型和一个微博模型。这两个模型都是可标记的。

用户.rb

 acts_as_taggable
 acts_as_taggable_on :tags

微博.rb

 acts_as_taggable
 acts_as_taggable_on :tags

标签是从 30 个不同“主题”的通用列表中强制执行的。

我正在尝试查找与用户标签相关的问题,即用户可能感兴趣的微博。

显然可以找到具有共同标签的用户

   @users = @user.find_related_tags

但是如何跨 2 个模型做到这一点。

我尝试过一些愚蠢的事情,比如

 micropost = Micropost.new
 micropost.tag_list = current_user.tag_list
 @questions_list = micropost.find_related_tags 

我希望@questions_list 是一系列相关的微博。这行不通

有什么想法吗?

【问题讨论】:

    标签: ruby-on-rails-4 model acts-as-taggable-on


    【解决方案1】:

    类似的东西呢

    Micropost.tagged_with(@user.tag_list, :on => :tags, :any => true)
    

    基于this section from the docs

    【讨论】:

    • 哎呀。我试过这个,但我的版本不太正确。先生,你救了我很多。非常感谢.....!
    • 我有一个模型Product,其中has_many :taxons, through: :classifications。如何实现acts_as_taggable_on实现对产品带分类名称的标注?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-29
    • 2010-12-26
    相关资源
    最近更新 更多