GithubHelp home page GithubHelp logo

Comments (3)

ssnickolay avatar ssnickolay commented on August 16, 2024 1

Hi @dolhishev! Your case is interesting and unusual but make sense, yeh.
First of all, if this approach with two different columns (id and uuid) is used for all your models, you can define your adapter:

class UUIDActiveRecordAdapter < Clowne::Adapters::ActiveRecord
  class << self
    def dup_record(record)
       super.tap do |clone|
         clone.uuid = nil
       end
    end
  end
end

# and 
class BaseCloner < Clowne::Cloner
  adapter UUIDActiveRecordAdapter
end

class SomeModelCloner < BaseCloner
  # declarations
end

See here and here.

It would be great to check if an association has dependent option and if an association is already present before clone associations step then raise an error for such idiots like me :)

Hm, maybe is too rude to raise an exception but we can handle this case and put a warning message. I'll think about it.
And thanks for the report 👍

from clowne.

curpeng avatar curpeng commented on August 16, 2024

I had debugged the issue a little bit and here is what I found:

This line is destroying association on source model:

  # Clowne::Adapters::ActiveRecord::Associations::HasOne
  record.__send__(:"#{association_name}=", child_clone)

Problem is that record.__send__(:"#{association_name}") is not nil, actually it's association of source object, so when we set new association to child_clone active record destroys source association because of 'dependent: :destroy' option.

To fix it, I think, we need to change the way how gem does initial copy of the object:

# Clowne::Adapters::Base
init_record = init_record(self.class.dup_record(source))

This line calls ActiveRecord's dup method, which duplicates a model + parent associations.

from clowne.

curpeng avatar curpeng commented on August 16, 2024

After a fresh look, I've realized what's going on:
I have a model with primary key :id and additional column uuid, my association looks like this:

has_one :image, dependent: :destroy, inverse_of: :post, foreign_key: :post_uuid, primary_key: :uuid

So when the cloning process starts, :id is set to nil, but uuid is not(I have used nullify :uuid), so newly created clone already has a relation of a source object and when clowne sets a cloned version of this association rails destroys original one due to dependent: :destroy option. I've fixed this problem by using init_as:

  init_as do |source|
    clone = source.dup
    clone.uuid = nil
    clone
  end

It would be great to check if an association has dependent option and if an association is already present before clone associations step then raise an error for such idiots like me :)
Thanks a lot for such nice gem!

from clowne.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.