Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You may get some good use out of the <a href="https://rubygems.org/gems/amoeba" rel="noreferrer">Amoeba gem</a> for ActiveRecord 3.2.</p> <p>It supports easy and automatic recursive duplication of <code>has_one</code>, <code>has_many</code> and <code>has_and_belongs_to_many</code> associations, field preprocessing and a highly flexible and powerful configuration DSL that can be applied both to the model and on the fly.</p> <p>be sure to check out the <a href="https://github.com/rocksolidwebdesign/amoeba#readme" rel="noreferrer">Amoeba Documentation</a> but usage is pretty easy...</p> <p>just</p> <pre><code>gem install amoeba </code></pre> <p>or add </p> <pre><code>gem 'amoeba' </code></pre> <p>to your Gemfile</p> <p>then add the amoeba block to your model and run the <code>dup</code> method as usual</p> <pre><code>class Post &lt; ActiveRecord::Base has_many :comments has_and_belongs_to_many :tags amoeba do enable end end class Comment &lt; ActiveRecord::Base belongs_to :post end class Tag &lt; ActiveRecord::Base has_and_belongs_to_many :posts end class PostsController &lt; ActionController def some_method my_post = Post.find(params[:id]) new_post = my_post.dup new_post.save end end </code></pre> <p>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:</p> <pre><code>class Post &lt; ActiveRecord::Base has_many :comments has_and_belongs_to_many :tags amoeba do include_field :comments end end </code></pre> <p>or using the exclusive syntax</p> <pre><code>class Post &lt; ActiveRecord::Base has_many :comments has_and_belongs_to_many :tags amoeba do exclude_field :comments end end </code></pre> <p>or by specifying which field types to recognize (and thusly copy)</p> <pre><code>class Post &lt; ActiveRecord::Base has_many :comments has_and_belongs_to_many :tags amoeba do recognize :has_and_belongs_to_many end end </code></pre> <p>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.</p> <p>Amoeba will also automatically recurse in to child records if you enable them</p> <pre><code>class Post &lt; ActiveRecord::Base has_many :comments amoeba do enable end end class Comment &lt; ActiveRecord::Base belongs_to :post has_many :ratings amoeba do enable end end class Rating &lt; ActiveRecord::Base belongs_to :comment end </code></pre> <p>You can also prefix fields with some extra data to indicate uniqueness</p> <pre><code>class Post &lt; ActiveRecord::Base has_many :comments amoeba do enable prepend :title =&gt; "Copy of " end end </code></pre> <p>and in addition to prepend you can also append to or run a regex on a given field</p> <p>Enjoy! :)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload