【问题标题】:amoeba not cloning all :has_many associations变形虫没有克隆所有:has_many 关联
【发布时间】:2017-03-12 04:38:16
【问题描述】:

我正在使用 Amoeba gem 克隆一个模型和所有孩子。 gem 运行良好,但有一个例外 - 有一个 :has_many 关联没有被拾取。

我的父模型是选项:

class Option < ActiveRecord::Base

has_many :products, as: :productable, dependent: :destroy
has_many :censusinfos, :autosave => true

belongs_to :rating

accepts_nested_attributes_for :censusinfos


amoeba do
  enable
end

# other code.....

产品正在被适当地克隆,但问题出在 :censusinfos。该模型定义为:

class Censusinfo < ActiveRecord::Base

has_many :census_sheets
has_many :census_fields
belongs_to :option

#other code......

CensusField 子项已正确复制,但未克隆 CensusSheet。

任何想法/想法为什么??

谢谢!

格雷格

【问题讨论】:

  • 你不应该通过包含在class Censusinfo `amoeba do enable end` 中来启用recursive copying of associations。我想你发现变形虫听到了吗? stackoverflow.com/questions/6711956/…
  • 你是对的!没有在 Censusinfo 协会中“启用”变形虫。
  • @Gpnect76 非常感谢。我发布了对您帖子的答案。

标签: ruby-on-rails ruby amoeba-gem


【解决方案1】:

我阅读了以下链接中的文档

ActiveRecord: How can I clone nested associations?

您是否应该通过在类 Censusinfo amoeba do enable end 中包含来启用关联的递归复制?

class Censusinfo < ActiveRecord::Base

has_many :census_sheets
has_many :census_fields
belongs_to :option

amoeba do
  enable
end

谢谢

法布里齐奥

【讨论】:

    【解决方案2】:

    我需要在 Censusinfo 中添加“启用”。下面的例子。感谢 Fabrizio!

    class Censusinfo < ActiveRecord::Base
    
    has_many :census_sheets
    has_many :census_fields
    belongs_to :option
    
    amoeba do
      enable
    end
    
    #other code...... 
    

    【讨论】:

      猜你喜欢
      • 2012-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-23
      • 1970-01-01
      • 1970-01-01
      • 2011-11-08
      相关资源
      最近更新 更多