-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Question] belongs_to IDs with nested models #51
Comments
Hey @vitobotta ! class A < ActiveRecord::Base
has_many :b_records, class_name: "B"
end
class B < ActiveRecord::Base
belongs_to :a_record, class_name: "A"
end And you want to do something like this: class ACloner < Clowne::Cloner
include_associations :b_records
end
a1 = A.sample
a1.b_record_ids = [B.sample.id, B.sample.id]
operation = ACloner.call(a1)
operation.persist
a2 = operation.to_record
puts a2.b_records_ids
# ??? If so, in this case we will have: a2.b_record_ids == a1.b_record_ids
# false because a2 clearly will have a relation with cloned b_records (not with a1.b_records). This relation cloning logic works as deep as you have enough imagination) p/s/ If I didn't understand you correctly or you have a more complex case - just write here and I will help if I can;) |
Hi @ssnickolay ! Thanks a lot for replying. I am trying to use this gem now and I am almost there. I have three models: ThemeVersion has many Templates, and Template has many TemplateVersions. These are the cloners: class ThemeVersionCloner < Clowne::Cloner
adapter :active_record
include_association :templates, clone_with: TemplateCloner
finalize do |_source, record, params|
record.current = false
record.description = params[:description]
record.preview_token = SecureRandom.hex(16)
end
end class TemplateCloner < Clowne::Cloner
adapter :active_record
include_association :template_versions, clone_with: TemplateVersionCloner
end class TemplateVersionCloner < Clowne::Cloner
adapter :active_record
finalize do |source, record, params|
record.path = _source.path.gsub(/\/\d+\//, "/#{record.template.theme_version.id}/")
end
end I am having two problems:
Thanks in advance! |
I got it working with BTW the Documentation link in the README is barely noticeable! Maybe it could be made more prominent. I didn't notice it before so I didn't know about |
Hm, sounds reasonable. I'll think what we can do. Thanks 👍
You can preload all associations using eager loading
I think this should helpful for you: https://clowne.evilmartians.io/#/parameters
You can try to implement a store to collect all changes and use bulk upsert (new Rails 6 feature): class TemplateVersionCloner < Clowne::Cloner
# adapter :active_record we can skip it
after_persist do |origin, clone, mapper:, template_version_store: |
cloned_version = mapper.clone_of(origin.bio)
template_version_store.unshift({
id: clone.id,
path: _source.path.gsub(/\/\d+\//, "/#{cloned_version.template.theme_version.id}/")
}) # add to the store what we need to change
end
end
template_version_store = []
theme = Theme.includes(templates: :template_versions).find(params[:id]) # preload what we will clone
operation = ThemeVersionCloner.call(theme, {template_version_store: template_version_store})
operation.persist
puts template_version_store
# list of collected changes
TemplateVersion.upsert_all(template_version_store, unique_by: :id) |
@ssnickolay It works beautifully, thanks a lot! |
Hi! I am planning a new feature for my app that will need cloning a structure with nested models. There are plan has_many/belongs_to and has_many :trhough associations.
My question: does clowne automatically set the correct/new parent IDs for child models?
Say I have an instance of A1, that has_many B1s. These B1s of course have A1's ID as the id for the belongs_to association. If I clone A1 to A2, A2 will have many B2s. Will these B2s have A2's ID as the parent ID, or A1's ID? And what if I have multiple nesting levels? And what about parent IDs in has_many :through associations?
I hope the question is clear. I am still thinking about how to implement this feature so I am trying to put the pieces together.
Thanks in advance!
The text was updated successfully, but these errors were encountered: