【问题标题】:Rails + Amoeba error: Uninitialized constantRails + Amoeba 错误:未初始化的常量
【发布时间】:2019-06-11 07:05:06
【问题描述】:

我需要在我的 Rails 应用程序中克隆一个模型,我正在使用 amoeba gem 来做同样的事情。

class Quiz
  belongs_to :scoring
  belongs_to :skill
  has_many :questions, :dependent => :destroy
  has_many :attempts, :dependent => :destroy
  has_many :quizzes_test_profiles, :dependent => :destroy
  has_many :test_profiles, :through => :quizzes_test_profiles
  has_many :evaluations_evaluation_sets, as: :resource

  amoeba do
    enable
  end
end

class EvaluationsEvaluationSet
  belongs_to :test_profile
  belongs_to :resource, polymorphic: true
  belongs_to :evaluation_set
end

我需要克隆 Quiz 模型及其所有嵌套关联。

class QuizzesController
  def duplicate_survey
    id = params[:id]
    original_survey = Survey.find(id)
    respond_to do |format|
      new_survey = original_survey.amoeba_dup
      new_survey.save
      if new_survey
        flash[:notice] = 'Survey successfully cloned.'
      else
        flash[:error]  = 'Survey could not be cloned'
      end
      format.html {redirect_to :back}
    end
  end
end

每当我执行上述代码时,我都会收到以下错误:

uninitialized constant Quiz::EvaluationsEvaluationSet

我不知道这里的错误在哪里。请告诉我如何纠正它。

【问题讨论】:

    标签: ruby-on-rails amoeba-gem


    【解决方案1】:

    你需要添加

    ```
     amoeba do
       enable
     end
    ```
    

    在调查模型中。

    仔细阅读https://github.com/amoeba-rb/amoeba 文档。 如果您有具有相应关联的克隆对象,那么您应该添加

     amoeba do
        exclude_association
        include_association
        nullify 
      end
    

    在克隆对象时使用此类预处理指令。

    首先尝试使用 rails 控制台使用 amoeba 克隆对象。

    【讨论】:

    • 我在测验模型中添加了amoeba do enable end。它在问题中。如果我使用include_association,我会得到macro 错误。
    • 我认为您因为has_many :evaluations_evaluation_sets, as: :resource 出现错误:资源,在克隆评估评估集时出现问题,请尝试将其从克隆中排除。并在保存对象时添加save!。跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-21
    相关资源
    最近更新 更多