【问题标题】:This method is only adding one tag, but I want multiple tags!这个方法只是添加一个标签,但我想要多个标签!
【发布时间】:2011-03-31 22:01:28
【问题描述】:

我的视频模型中有这个方法:

def assign_topics
  if @topic_names
    self.topics = @topic_names.map do |name|
      Topic.find_or_create_by_name(name)
    end
  end
end

topic_names 作为虚拟属性。这是怎么回事,每次我添加一个新主题时,似乎都会用新主题替换旧主题。我不希望这种情况发生。我希望它添加一个新主题。我该怎么做?

请注意,这不是我的关联问题。我已经通过关联正确实现了 has_many。

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 tags


    【解决方案1】:

    没关系,我想通了:

    def assign_topics
      if @topic_names
        self.topics << @topic_names.map do |name|
          Topic.find_or_create_by_name(name)
        end
      end
    end
    

    【讨论】:

      猜你喜欢
      • 2021-12-23
      • 1970-01-01
      • 2012-08-17
      • 1970-01-01
      • 2022-11-16
      • 1970-01-01
      • 1970-01-01
      • 2016-01-30
      • 2020-08-11
      相关资源
      最近更新 更多