2

I have an App with og_objects, og_actions, and stories I have created a way to create a clone of this app.

I am now trying to duplicate the og_objects, og_actions and stories into the clone, but I am getting stuck.

I am getting stuck in two places.
1. When I create a clone, the objects,actions,stories get moved to the new clone, but they are not duplicated, meaning the parent app loses them.
2. I get an error that my new clone does not have og_objects property. Specifically the error is:

   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'

This is the code I have:

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:

In my controller, I have a generator, and I have the following code:

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
Bob
  • 1,605
  • 2
  • 18
  • 33

1 Answers1

1

To get this to work, I made use of the first_or_initialize method, and created the relationships if I couldn't find a record, otherwise I updated the record.

I changed the populate method to the following:

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

I also removed the creation of these symbols in the AppGenerator like this:

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  
Bob
  • 1,605
  • 2
  • 18
  • 33