Skip to content
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

How to access the target cloned parent? #19

Closed
pomartel opened this Issue Mar 27, 2018 · 12 comments

Comments

Projects
None yet
3 participants
@pomartel
Copy link

pomartel commented Mar 27, 2018

When doing a deep cloning of an ActiveRecord object, I would like a child to access it's cloned parent in the finalize block. Instead when I access the parent association, it is linked to the source parent.

Is this there a way for the child of deep cloned object to retrieve it's parent association which is also a cloned object?

@palkan

This comment has been minimized.

Copy link
Owner

palkan commented Mar 28, 2018

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?

@pomartel

This comment has been minimized.

Copy link
Author

pomartel commented Mar 28, 2018

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?

@palkan

This comment has been minimized.

Copy link
Owner

palkan commented Mar 28, 2018

Doesn't like the intended behaviour to me. @ssnickolay Could you, please, take a look?

@palkan palkan added the bug label Mar 28, 2018

@ssnickolay ssnickolay self-assigned this Mar 28, 2018

@ssnickolay

This comment has been minimized.

Copy link
Collaborator

ssnickolay commented Mar 28, 2018

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

  1. We clone nested record with some cloner. In this step, you use the nested record as a nested source.dup so all of his foreign_keys are equal to nested source's foreign_keys and you get this behavior:

when I access the parent association, it is linked to the source parent.

  1. We always nullify foreign_key of an object that is the result of step-1.
  2. 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.

@pomartel

This comment has been minimized.

Copy link
Author

pomartel commented Mar 28, 2018

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. :)

@ssnickolay

This comment has been minimized.

Copy link
Collaborator

ssnickolay commented Mar 29, 2018

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?

@pomartel

This comment has been minimized.

Copy link
Author

pomartel commented Mar 29, 2018

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? :)

@ssnickolay

This comment has been minimized.

Copy link
Collaborator

ssnickolay commented Mar 29, 2018

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
@pomartel

This comment has been minimized.

Copy link
Author

pomartel commented Mar 29, 2018

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.

@ssnickolay

This comment has been minimized.

Copy link
Collaborator

ssnickolay commented Mar 29, 2018

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!

@pomartel

This comment has been minimized.

Copy link
Author

pomartel commented Mar 29, 2018

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.

@ssnickolay ssnickolay added enhancement question and removed bug labels Mar 30, 2018

@ssnickolay ssnickolay referenced this issue Dec 28, 2018

Merged

Post-processing #21

7 of 7 tasks complete
@ssnickolay

This comment has been minimized.

Copy link
Collaborator

ssnickolay commented Mar 20, 2019

Closed by #21

@ssnickolay ssnickolay closed this Mar 20, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.