我.clone -ing在轨记录...
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
协会,实地preprocessing以及可以同时适用于该模型,并在飞行中。
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.
一定要检查出href="https://github.com/rocksolidwebdesign/amoeba#readme">阿米巴文档但用法是pretty的方便......
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
或使用专用的语法
or using the exclusive syntax
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
您也可以$一些额外的数据P $ PFIX领域,以表明其唯一
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
和除prePEND还可以追加或在某一领域运行正则表达式
and in addition to prepend you can also append to or run a regex on a given field
享受! :)