我想制作一个 activerecord 记录的副本,更改流程中的单个字段(除了 ID)。实现这一目标的最简单方法是什么?

我意识到我可以创建一条新记录,然后迭代每个字段,逐个字段复制数据 - 但我认为必须有一种更简单的方法来做到这一点......

例如:

 @newrecord=Record.copy(:id)  *perhaps?*
有帮助吗?

解决方案

要获取副本,请使用克隆(或 Rails 3.1 的 dup)方法:

# rails < 3.1
new_record = old_record.clone

#rails >= 3.1
new_record = old_record.dup

然后您可以更改您想要的任何字段。

ActiveRecord 覆盖内置 Object#clone 为您提供一个带有未分配 ID 的新记录(未保存到数据库中)。
请注意,它不会复制关联,因此如果需要,您必须手动执行此操作。

Rails 3.1 克隆是浅拷贝,请使用 dup 代替...

其他提示

根据您的需要和编程风格,您还可以使用类的新方法和合并的组合。因为缺乏更好的 简单的 例如,假设您计划在某个日期执行一项任务,并且希望将其复制到另一个日期。任务的实际属性并不重要,因此:

old_task = Task.find(task_id)
new_task = Task.new(old_task.attributes.merge({:scheduled_on => some_new_date}))

将创建一个新任务 :id => nil, :scheduled_on => some_new_date, ,以及所有其他属性与原始任务相同。使用Task.new,您必须显式调用save,因此如果您希望自动保存它,请将Task.new 更改为Task.create。

和平。

您可能还喜欢 阿米巴宝石 对于 ActiveRecord 3.2。

就您而言,您可能想利用 nullify, regex 或者 prefix 配置 DSL 中可用的选项。

它支持简单且自动的递归复制 has_one, has_manyhas_and_belongs_to_many 关联、现场预处理和高度灵活且强大的配置 DSL,可应用于模型和动态。

请务必查看 阿米巴文档 但使用非常简单...

只是

gem install amoeba

或添加

gem 'amoeba'

到您的 Gemfile

然后将阿米巴块添加到您的模型中并运行 dup 方法如常

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

您还可以通过多种方式控制复制哪些字段,但例如,如果您想防止注释重复,但又想维护相同的标签,您可以执行以下操作:

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

  amoeba do
    exclude_field :comments
  end
end

您还可以预处理字段以帮助指示前缀和后缀以及正则表达式的唯一性。此外,还有许多选项,因此您可以根据您的目的以最易读的风格进行编写:

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

  amoeba do
    include_field :tags
    prepend :title => "Copy of "
    append :contents => " (copied version)"
    regex :contents => {:replace => /dog/, :with => "cat"}
  end
end

递归复制关联很容易,只需在子模型上启用阿米巴即可

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

配置 DSL 还有更多选项,因此请务必查看文档。

享受!:)

使用 ActiveRecord::Base#dup 如果你不想复制id

我通常只是复制属性,更改我需要更改的内容:

new_user = User.new(old_user.attributes.merge(:login => "newlogin"))

如果您需要带有关联的深层副本,我推荐 深克隆 宝石。

简单的方法是:

#your rails >= 3.1 (i was done it with Rails 5.0.0.1)
  o = Model.find(id)
 # (Range).each do |item|
 (1..109).each do |item|
   new_record = o.dup
   new_record.save
 end

或者

# if your rails < 3.1
 o = Model.find(id)
 (1..109).each do |item|
   new_record = o.clone
   new_record.save
 end     

在 Rails 5 中,您可以像这样简单地创建重复对象或记录。

new_user = old_user.dup

您还可以检查 可继承的行为 宝石。

“Acts As Inheritable 是专门为 Rails/ActiveRecord 模型编写的 Ruby Gem。它旨在与 自我参考协会, ,或者具有共享可继承属性的父模型的模型。这将使您可以从父模型继承任何属性或关系。”

通过增加 acts_as_inheritable 对于您的模型,您将可以访问以下方法:

继承属性

class Person < ActiveRecord::Base

  acts_as_inheritable attributes: %w(favorite_color last_name soccer_team)

  # Associations
  belongs_to  :parent, class_name: 'Person'
  has_many    :children, class_name: 'Person', foreign_key: :parent_id
end

parent = Person.create(last_name: 'Arango', soccer_team: 'Verdolaga', favorite_color:'Green')

son = Person.create(parent: parent)
son.inherit_attributes
son.last_name # => Arango
son.soccer_team # => Verdolaga
son.favorite_color # => Green

继承关系

class Person < ActiveRecord::Base

  acts_as_inheritable associations: %w(pet)

  # Associations
  has_one     :pet
end

parent = Person.create(last_name: 'Arango')
parent_pet = Pet.create(person: parent, name: 'Mango', breed:'Golden Retriver')
parent_pet.inspect #=> #<Pet id: 1, person_id: 1, name: "Mango", breed: "Golden Retriver">

son = Person.create(parent: parent)
son.inherit_relations
son.pet.inspect # => #<Pet id: 2, person_id: 2, name: "Mango", breed: "Golden Retriver">

希望这可以帮到你。

由于可能有更多逻辑,因此在复制模型时,我建议创建一个新类,在其中处理所有需要的逻辑。为了缓解这个问题,有一个 gem 可以帮助您: 小丑

根据他们的文档示例,对于用户模型:

class User < ActiveRecord::Base
  # create_table :users do |t|
  #  t.string :login
  #  t.string :email
  #  t.timestamps null: false
  # end

  has_one :profile
  has_many :posts
end

您创建克隆器类:

class UserCloner < Clowne::Cloner
  adapter :active_record

  include_association :profile, clone_with: SpecialProfileCloner
  include_association :posts

  nullify :login

  # params here is an arbitrary Hash passed into cloner
  finalize do |_source, record, params|
    record.email = params[:email]
  end
end

class SpecialProfileCloner < Clowne::Cloner
  adapter :active_record

  nullify :name
end

然后使用它:

user = User.last
#=> <#User(login: 'clown', email: 'clown@circus.example.com')>

cloned = UserCloner.call(user, email: 'fake@example.com')
cloned.persisted?
# => false

cloned.save!
cloned.login
# => nil
cloned.email
# => "fake@example.com"

# associations:
cloned.posts.count == user.posts.count
# => true
cloned.profile.name
# => nil

从项目中复制的示例,但它将给出您可以实现的目标的清晰愿景。

为了快速简单的记录,我会选择:

Model.new(Model.last.attributes.reject {|k,_v| k.to_s == 'id'}

这是重写 ActiveRecord 的示例 #dup 自定义实例复制并包含关系复制的方法:

class Offer < ApplicationRecord
  has_many :offer_items

  def dup
    super.tap do |new_offer|

      # change title of the new instance
      new_offer.title = "Copy of #{@offer.title}"

      # duplicate offer_items as well
      self.offer_items.each { |offer_item| new_offer.offer_items << offer_item.dup }
    end
  end
end

笔记:此方法不需要任何外部 gem,但需要更新的 ActiveRecord 版本 #dup 实施的方法

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top