如何在rails中克隆记录及其关系?

时间:2015-12-07 16:32:59

标签: ruby-on-rails ruby-on-rails-4 clone dup

我的应用有og_objectsog_actionsstories 我创建了一种创建此应用程序克隆的方法。

我现在正在尝试将og_objectsog_actionsstories复制到克隆中,但我遇到了问题。

我被困在两个地方  1.当我创建一个克隆时,对象,动作,故事都会移动到     新克隆,但它们没有重复,这意味着父应用程序丢失     它们。
 2.我收到一个错误,我的新克隆没有og_objects属性。特别是错误是:

   ActiveModel::MissingAttributeError (can't write unknown attribute `og_actions'):
  app/models/app.rb:39:in `block in populate'
  app/models/app.rb:36:in `populate'
  app/models/app_generator.rb:15:in `generate'
  app/controllers/apps_controller.rb:12:in `create'

这是我的代码:

class App < ActiveRecord::Base
  validates :name, :presence => true, :uniqueness => {:scope => :dev_mode}
  validates :user_id, :presence => true
  before_validation :validate_app

  belongs_to :user
  has_many :og_actions
  has_many :og_objects
  has_many :stories
  has_many :log_reports
  has_many :clones, class_name: "App", foreign_key: "parent_id"

  belongs_to :parent_app, class_name: "App"

  def self.dev_modes
   ['development', 'stage', 'production']
  end

  def is_clone?
    !!self.parent_id
  end

  def clone_ids
    if !is_clone?
      self.clones.all.map(&id)
    end
  end

  def populate
    p "doing populate"
    p self.clones
    p "original clones"
    new_og_actions = self.og_actions.dup
    new_og_objects = self.og_objects.dup
    new_stories = self.stories.dup
    self.clones.each do |c|
      p "updating ->"
      p c
      c[:og_actions] = new_og_actions
      c[:og_objects] = new_og_objects
      c[:stories] = new_stories

    end 
  end


#Other irrelevant code here:

在我的控制器中,我有一个生成器,我有以下代码:

if @parent_id.length > 0
         parent_app = App.find(@parent_id)
         App.create_with(og_actions: parent_app.og_actions.dup, og_objects: parent_app.og_objects.dup, stories:parent_app.stories.dup, parent_id: @parent_id, user_id: @user_id, app_id:@app_id, app_secret: @app_secret).find_or_create_by!(name: @name, dev_mode: 'clone')
         parent_app.populate
    else

1 个答案:

答案 0 :(得分:1)

为了使其工作,我使用了first_or_initialize方法,并在我找不到记录时创建了关系,否则我更新了记录。

我将populate方法更改为以下内容:

def populate
    p "doing populate"
    p self.clones
    p "original clones"
    new_og_objects = self.og_objects.dup
    new_stories = self.stories.dup
    self.clones.each do |c|
      p "updating ->"
      self.og_actions.each do | oa |
        new_og_actions = OgAction.create(oa.attributes.merge({app_id:c.id, id: nil }))
        c.og_actions.first_or_initialize(new_og_actions)
      end
      self.og_objects.each do |oo|
        new_og_objects = OgObject.create(oo.attributes.merge({app_id:c.id, id: nil }))
        c.og_objects.first_or_initialize(new_og_objects)
      end   
      self.stories.each do | s|
        new_stories = Story.create(s.attributes.merge({app_id:c.id, id: nil }))
        c.stories.first_or_initialize(new_stories)
      end
      p c
    end 
  end

我还在AppGenerator中删除了这些符号的创建,如下所示:

if @parent_id.length > 0
      parent_app = App.find(@parent_id)
      App.create_with(parent_id: @parent_id, user_id: @user_id, app_id:@app_id, app_secret: @app_secret).find_or_create_by!(name: @name, dev_mode: 'clone')
      parent_app.populate
    else