Ruby on rails 创建子记录时跳过纸面文档的版本创建
我有一个简单的模型,带有创建关联记录的after_create过滤器Ruby on rails 创建子记录时跳过纸面文档的版本创建,ruby-on-rails,ruby-on-rails-3,paper-trail-gem,Ruby On Rails,Ruby On Rails 3,Paper Trail Gem,我有一个简单的模型,带有创建关联记录的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_ty
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的子类,而是属于subject的。他们也有一个文件线索,应该从创作过程开始到主题进行版本控制
class Topic
belongs_to :subject
end
主题是主语的一个子类吗?如果是这样,主题将继承回调,它将由self.topics.createTopic触发。createTopic是一个单独的模型,而不是子类。更新的问题,感觉好像我没有提供所有内容。当我想从一开始就对主题本身进行版本控制(创建主题时有创建事件)时,这是否有效?我不这么认为。否则,我读到,如果您创建主题并手动将其外键设置在主题上,则不会触发主题的书面记录。明白吗?这两种方法都不能按预期的方式工作。有趣的是:当我声明
创建主题时
public并就此主题调用它,我没有得到新版本(因为:skip
数组)。这实际上只是创建回调。我没有任何其他在保存后或创建后的回调会影响这一点。
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