【问题标题】:ActiveRecord: Create Duplicates ActiveRecord ResultsActiveRecord:创建重复的 ActiveRecord 结果
【发布时间】:2013-02-14 10:48:16
【问题描述】:

我想按需复制几条记录。这些记录为新用户播种了示例记录,他们可能会随着时间的推移对其进行自定义。我从以下开始:

MaintenanceSchedule.find_each do |schedule|
  @example=MaintenanceSchedule.new(schedule)
  @example.org_id=current_user.active_org.id
  @example.save
end

这是一个很长的镜头,它在第二行按预期失败了:

undefined method `stringify_keys' for #<MaintenanceSchedule:0x4c4d450>

不要介意错误。复制记录的最佳方法是什么?

【问题讨论】:

    标签: ruby-on-rails-3 activerecord


    【解决方案1】:

    以下方法可以解决问题,尽管我很乐意找到更有效的方法:

    MaintenanceSchedule.find_each do |schedule|
      @example=schedule.dup
      @example.org_id=current_user.active_org.id
      @example.save
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-10
      • 2012-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多