在rails中克隆记录,可以克隆关联和深层复制吗? [英] Cloning a record in rails, is it possible to clone associations and deep copy?

查看:181
本文介绍了在rails中克隆记录,可以克隆关联和深层复制吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是.clone-rails中的记录...

I'm .clone -ing a record in rails...

  new_blerg = Blerg.find(1).clone

此记录具有加载和加载的关联,这些关联甚至具有关联。

This record has loads and loads of associations, and those associations even have associations.

有没有办法深入复制一个记录并将其克隆,以便克隆所有这些关联?

Is there a way to deep-copy a record and clone it so it is cloned with all of those associations too?

推荐答案

您可以从ActiveRecord的阿米巴宝石获得一些好用3.2。

You may get some good use out of the Amoeba gem for ActiveRecord 3.2.

它支持轻松自动递归复制 has_one has_many has_and_belongs_to_many 关联,字段预处理和高度灵活和强大的配置DSL,可以同时应用于模型和即​​时。

It supports easy and automatic recursive duplication of has_one, has_many and has_and_belongs_to_many associations, field preprocessing and a highly flexible and powerful configuration DSL that can be applied both to the model and on the fly.

请务必查看阿米巴文档,但使用情况是p简单...

be sure to check out the Amoeba Documentation but usage is pretty easy...

只是

gem install amoeba

或添加

gem 'amoeba'

到您的Gemfile

to your Gemfile

然后将变形虫块添加到您的模型,并像往常一样运行 dup 方法

then add the amoeba block to your model and run the dup method as usual

class Post < ActiveRecord::Base
  has_many :comments
  has_and_belongs_to_many :tags

  amoeba do
    enable
  end
end

class Comment < ActiveRecord::Base
  belongs_to :post
end

class Tag < ActiveRecord::Base
  has_and_belongs_to_many :posts
end

class PostsController < ActionController
  def some_method
    my_post = Post.find(params[:id])
    new_post = my_post.dup
    new_post.save
  end
end

您的新帖子应该具有最初与之关联的所有标签,所有评论都应该被复制以及。您可以通过DSL禁用各种记录的重复,您可以在文档中阅读,但例如,如果您想保留标签,但不能评论您可以执行的操作:

Your new post should have all the tags that were originally associated with it, and all the comments should be duplicated as well. You can disable the duplication of various records through the DSL, which you can read about in the documentation, but for example, if you wanted to keep the tags, but not the comments you could do something like this:

class Post < ActiveRecord::Base
  has_many :comments
  has_and_belongs_to_many :tags

  amoeba do
    include_field :comments
  end
end

或使用独家语法

class Post < ActiveRecord::Base
  has_many :comments
  has_and_belongs_to_many :tags

  amoeba do
    exclude_field :comments
  end
end

或指定要识别哪些字段类型(因此复制)

or by specifying which field types to recognize (and thusly copy)

class Post < ActiveRecord::Base
  has_many :comments
  has_and_belongs_to_many :tags

  amoeba do
    recognize :has_and_belongs_to_many
  end
end

这些各种选项都应该导致将与新帖相同的标签与新帖相关联,但不会重复

each of these various options should result in re-associating the new post with the same tags as the old post, but without duplicating the comments.

如果您启用它们,变形虫也会自动递归到子记录中。

Amoeba will also automatically recurse in to child records if you enable them

class Post < ActiveRecord::Base
  has_many :comments

  amoeba do
    enable
  end
end

class Comment < ActiveRecord::Base
  belongs_to :post
  has_many :ratings

  amoeba do
    enable
  end
end

class Rating < ActiveRecord::Base
  belongs_to :comment
end

您还可以前缀字段有一些额外的数据表示唯一性

You can also prefix fields with some extra data to indicate uniqueness

class Post < ActiveRecord::Base
  has_many :comments

  amoeba do
    enable
    prepend :title => "Copy of "
  end
end

除了前置您可以也附加到或运行正则表达式给定的字段

and in addition to prepend you can also append to or run a regex on a given field

享受! :)

这篇关于在rails中克隆记录,可以克隆关联和深层复制吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆