clowne-rb/clowne

How to access the target cloned parent?

Closed this issue ยท 12 comments

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?

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?

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?

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

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.

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

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?

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

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

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.

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!

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.

Closed by #21