GithubHelp home page GithubHelp logo

Comments (12)

ssnickolay avatar ssnickolay commented on July 17, 2024 1

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.

from clowne.

ssnickolay avatar ssnickolay commented on July 17, 2024 1

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.

palkan avatar palkan commented on July 17, 2024

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.

pomartel avatar pomartel commented on July 17, 2024

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.

palkan avatar palkan commented on July 17, 2024

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

from clowne.

pomartel avatar pomartel commented on July 17, 2024

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.

ssnickolay avatar ssnickolay commented on July 17, 2024

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.

pomartel avatar pomartel commented on July 17, 2024

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.

ssnickolay avatar ssnickolay commented on July 17, 2024

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.

pomartel avatar pomartel commented on July 17, 2024

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.

pomartel avatar pomartel commented on July 17, 2024

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.

ssnickolay avatar ssnickolay commented on July 17, 2024

Closed by #21

from clowne.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.