0

我有一个带有创建关联记录的 after_create 过滤器的简单模型。

class Subject
  after_create :create_topics!

  has_paper_trail :on => [:create, :update],
                  :ignore => [:topics]

  private

  def create_topics!
    self.account.default_topics_for_subject_type(self.subject_type).each do |topic|
      self.topics.create!({:name => topic.name})
    end
  end
end

然而,创建Subject例如将创建两个主题的 now 会导致同一主题的两个版本,即主题更改create之前和之后的版本。update

关于如何解决这个问题的任何想法?

更新
主题模型不是主题的子类,而是属于它。它们也有一个 paper_trail 并且应该从创建过程的开始到主题进行版本控制。

class Topic
  belongs_to :subject
end
4

1 回答 1

1
private

def create_topics!
  account.default_topics_for_subject_type(subject_type).each_with_index do |topic, index|
    if index == 0
      create_topic!(topic)
    else
      without_versioning { create_topic!(topic) }
    end
  end
end

def create_topic!(topic)
  self.topics.create!({:name => topic.name})
end
于 2012-09-09T22:54:51.283 回答