Comments (12)
Actually, it is not a bug.
What's happened step-by-step:
https://github.com/palkan/clowne/blob/master/lib/clowne/adapters/active_record/associations/has_many.rb#L10-L12
- We clone nested record with some cloner. In this step, you use the
nested record
as anested source.dup
so all of hisforeign_key
s are equal tonested source
'sforeign_key
s and you get this behavior:
when I access the parent association, it is linked to the source parent.
- We always nullify
foreign_key
of an object that is the result of step-1. - Add (remember) cloned record to parent's association.
========
PROFIT!
For our main goal this is enough, but, @pomartel, I understand your problem and it would be useful to get access to parent cloned object. We will think how it can be implemented.
from clowne.
I got it, we had exactly the same issue on our project when we use Clowne.
Now we are processing this case outside Clowne:
# pseudocode:
class ClonePost
def self.call(post)
# get clone
cloned_post = PostCloner.call(post)
# save it
cloned_post.save
# prepare children
grouped_tags = cloned_post.tags.index_by(&:name)
# and fix relations
cloned_post.comments.each do |comment|
# current_tag is broken and related to source post's tag
current_tag = comment.tag
comment.update_attributes(tag_id: grouped_tags[current_tag.name])
end
end
end
but this is boilerplate and we already had problems (when we missed editing relations) with this approach.
Actually, we discussed this problem with @palkan a month ago and I think we will do some mechanism to solve this issue because now it is clear that this is not only our problem.
@pomartel many thanks!
from clowne.
Hi @pomartel!
What do you mean by
Instead when I access the parent association, it is linked to the source parent.
?
Could you, please, provide a code example?
from clowne.
Here's an example where if I clone a user, I expected to be able to retrieve the cloned user from the finalize
block in the PostCloner
:
class UserCloner < Clowne::Cloner
include_association :posts
finalize do |_source, record, params|
record.posts # correctly links to the new children posts
end
end
class PostCloner < Clowne::Cloner
finalize do |_source, record, params|
record.user # should point to the new user record but it points to the source user instead
end
end
Is this the intented behaviour? Is there another way for the child to access its cloned parent?
from clowne.
Doesn't like the intended behaviour to me. @ssnickolay Could you, please, take a look?
from clowne.
Got it! I thought it might be some kind of chicken and egg problem. Still, it's a great gem and many thanks for releasing it. If you ever find a way to solve this, let me know. :)
from clowne.
it's a great gem and many thanks for releasing
Nice to hear (:
If you ever find a way to solve this, let me know. :)
Just for information - what do you want to do with new parent record inside "deep finalize"? Change or get some data?
from clowne.
I need to get some data. Actually the parent clones an association that is shared with the child. Here's an example where a post
and its comments
share the same group of tags
that is also cloned. The parent clones the tags
but the association between cloned tags
and the cloned comments
must also be made.
class Post
has_many :comments, :tags
end
class Comment
belongs_to :tag
end
class PostCloner < Clowne::Cloner
include_association :tags
include_association :comments
end
class CommentCloner < Clowne::Cloner
finalize do |_source, record, params|
record.post.tags # Access the cloned tags to create the association
end
I could probably do this in the finalize block of the parent instead but it's impractical for different reasons (the child can be cloned independently from the parent).
Does that make any sense? :)
from clowne.
Hm, do you try to restore (or save) "relations" between cloned comments and tag (which must be taken from the post), right?
class Post
has_many :comments, :tags
end
class Comment
belongs_to :tag
end
class PostCloner < Clowne::Cloner
include_association :tags
include_association :comments
end
class CommentCloner < Clowne::Cloner
finalize do |source, record, params|
# this is pseudo code and it does not work!
# only for demonstrating my thoughts:
cloned_tag_from_post = record.post.tags.detect { |tag| tag.name == source.tag.name }
self.tag_id = cloned_tag_from_post.id
end
end
from clowne.
Yes, that pseudo-code is pretty much exactly how I was trying to proceed! But record.post.tags
returns the source tags instead of the record cloned tags as I was expecting it.
from clowne.
Awesome, I just did something similar and it works like a charm. 🎉 Can't believe I didn't know about index_by
. That's quite handy. You learn new stuff everyday!
Thanks for your help and I'll be very interested if you find a way to fit a solution into the gem.
from clowne.
Closed by #21
from clowne.
Related Issues (20)
- Nested params HOT 14
- Multiple Databases HOT 1
- 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
- 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
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.