将现有记录附加到现有模型的集合

时间:2014-12-16 23:38:00

标签: ruby-on-rails

我有一个像这样的协会:

class Contact < ActiveRecord::Base
  has_many :contact_classifications
  has_many :classifications, through: :contact_classifications
end

class ContactClassification < ActiveRecord::Base
  belongs_to :contact
  belongs_to :classification
end

class Classification < ActiveRecord::Base
  has_many :contact_classifications
  has_many :contacts, through: :contact_classifications
end

我有现有联系人和现有分类。当我使用以下代码时:

contact = Contact.find(id)
classification = Classification.find(1)
contact.classifications << classification

它将记录插入到联接表ContactClassification中。我想在两个现有记录之间建立关联,而不创建连接记录,直到在其中一个记录上调用save之后。

我怎样才能做到这一点?

0 个答案:

没有答案
相关问题