Comments (3)
OMG! This is amazing! Thank you so much!
from clowne.
Hey @gee-forr .
Hm, you have an interesting case...AFAIU you could try to do something like this:
class Course < ApplicationRecord
has_many :course_modules
# other associations
end
class CourseSnapshot < Course
self.table_name = 'course_snapshots'
# foreign key to keep tables equal between each other
has_many :course_modules, class_name: 'CourseModuleSnapshot', foreign_key: :course_id
# other associations with overrided class_name & foreign_key options
end
class CourseModule < ApplicationRecord; end
class CourseModuleSnapshot < CourseModule
self.table_name = 'course_module_snapshots'
end
# Magic here:
class SnapshotAdapter < Clowne::Adapters::ActiveRecord
class << self
def dup_record(record)
# we cannot mix snapshot and origin models because of https://api.rubyonrails.org/classes/ActiveRecord/AssociationTypeMismatch.html
# so we override dup logic to always have snap models
# if you have witelisted models that should be cloned as is
# you can `return super if %w(SomeModel).include?(record.class)`
target_class = "#{record.class.name}Snapshot".constantize
target_class.new(record.dup.attributes).tap do |clone|
# you can skip these lines if you dont use after_persist hook (see https://clowne.evilmartians.io/#/after_persist)
operation = operation_class.current
operation.add_mapping(record, clone)
end
end
end
end
class CourseSnapshoter < BasePostCloner
adapter SnapshotAdapter # override default adapter
include_association :course_modules
end
# and use
CourseSnapshoter.call(Course.last).to_record
# => <# CourseSnapshote ... >
I checked locally with a similar example and everything seems to work
from clowne.
@ssnickolay thanks answering this 2 years back 😆 .. it has partially addressed a problem I am having trying to use clowne.
When I clone an AR model into a different one (as demoed above), I also need to rename an association.. is this possible?
from clowne.
Related Issues (20)
- Nested params HOT 14
- Multiple Databases HOT 1
- How to access the target cloned parent? HOT 12
- Plans to support belongs to and has many through? HOT 1
- `dependent: :destroy` option on association destroys models related to source after cloning HOT 3
- 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
- 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
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.