在rails中克隆记录,是否可以克隆关联和深拷贝? [英] Cloning a record in rails, is it possible to clone associations and deep copy?

查看:26
本文介绍了在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 的 Amoeba gem.

它支持 has_onehas_manyhas_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.

请务必查看 Amoeba 文档,但使用起来非常简单..

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 也会自动递归到子记录中

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天全站免登陆