Rails cloning objects question
Say I have a blog post with comments in rails, how do I clone it so I have another blog post with comments, both stored as new objects in the database with their own ids?
I know about the clone command, but it doe开发者_JAVA百科sn't seem to copy over the comments - only links to them.
Is there a plugin or an easy way to do this in rails?
Thanks!
Since deep copying/cloning is going to look different for every Model class, it's usually left as an exercise to the developer. Here are two ways:
Override clone (could be dangerous if you don't always want this behavior)
class Post ... def clone new_post = super new_post.comments = comments.collect { |c| c.clone } new_post end ... end
Create a deep_clone or deep_copy method and call it specifically
class Post ... def deep_clone new_post = clone new_post.comments = comments.collect { |c| c.clone } new_post end ... end
Both of these guarantee the returned Post object and all its comments will be distinct entities in db (once you call save on the Post, of course).
Why do you want to use plugins? It's really very simple.
- clone the master object, the
Post
- iterate over each comment in the master object, clone each comment and append it to the cloned post
- save the post, all comments will be saved and attached to that post
Define a deep copy method
class BlogPost
...
def deep_copy
Marshal::load(Marshal.dump(self))
end
...
end
This should do the trick! Edit : Just to clarify, what you are doing with this method is basically serializing your object to memory and then deserializing it in a new memory location, effectively creating a new object with the same field values as the original one.
精彩评论