has_and_belongs_to_many在模型中创建重复条目

时间:2015-05-01 06:41:36

标签: ruby activerecord

我有三个表问题标签,&的 IssuesLabel

issues.rb

class Issue < ActiveRecord::Base
  has_and_belongs_to_many :labels
end

labels.rb

class Label < ActiveRecord::Base
  has_and_belongs_to_many :issues
end

issues_label.rb

class IssuesLabel < ActiveRecord::Base
  belongs_to :issue
  belongs_to :label
end

当我致电issue.labels.find_or_create_by(name: 'bug')一个问题时, 和issue.labels.find_or_create_by(name: 'bug')针对其他问题,它会在Labels表中为bug

创建两个不同的记录

我希望它能找到现有的bug记录,并在IssuesLabel联接表中添加一个条目。我在这里缺少什么?

0 个答案:

没有答案