Comments (3)
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
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.
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.
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)
- Multiple Databases HOT 1
- How to access the target cloned parent? HOT 12
- Plans to support belongs to and has many through? HOT 1
- Is there support for self-referential tables HOT 1
- Weird bug upgrading from 0.2.0 to 1.1.0 HOT 2
- Active Storage & Action Text HOT 4
- Migrate to Standard HOT 3
- Association couldn't be found, Rails 4.1.8 HOT 3
- [Question] belongs_to IDs with nested models HOT 5
- polymorphic association HOT 2
- Is it possible to clone a record to a different table? HOT 3
- Which association is created first if you have multiple associations? HOT 2
- Ruby 3.0 compatibility HOT 5
- Is there possible to use traits in trait?
- Release with mapper access in `after_clone` HOT 2
- Nested include_association duplication HOT 7
- Is it possible to ignore an attribute? HOT 3
- Question HOT 1
- Question: Dynamic traits for child cloners
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from clowne.