【问题标题】:import with recursive using activerecord-import gem: has_many through association not working使用 activerecord-import gem 递归导入:has_many 通过关联不起作用
【发布时间】:2019-07-19 15:36:37
【问题描述】:

尝试使用“activerecord-import gem”将 CSV 文件导入数据库。 我有以下型号

question.rb

require 'csv'
class Question < ApplicationRecord
    has_many :question_answers, dependent: :destroy
    has_many :answers, through: :question_answers
    belongs_to :category
    belongs_to :product

answer.rb

class Answer < ApplicationRecord
    has_many :question_answers, dependent: :destroy
    has_many :questions, through: :question_answers
 end

question_answer.rb

class QuestionAnswer < ApplicationRecord
    belongs_to :question
    belongs_to :answer
end

以下方法是处理 CSV 数据并准备使用 ActiveRecord import gem 保存

def self.from_csv(file)
        questions = []
        CSV.foreach(file.path, headers: true) do |row|
            category = Category.find_by(name: row['category'].strip)
            product = Product.find_by(title: row['product'].strip)
            parent_q = Question.find_by(qname: row['parent'])
            question = Question.new(
                question: row['question'],
                qtype: row['qtype'],
                tooltip: row['tooltip'],
                parent: parent_q,
                position: row['position'],
                qname: row['qname'],
                category_id: category.id,
                product_id: product.id,
                state_id: row['state_id'],
                explanation: row['explanation']

            )
            answers = row['answers'].split(" | ") if row['answers'].present?

            if answers.present?
                answers.each do |a_str|
                    answer_arr = a_str.split(',')
                    question.answers.build(answer: answer_arr[0] || "", value: answer_arr[1] || "", pdf_parag: answer_arr[2] || "", key: answer_arr[3] || "", position: answer_arr[4] || "", note: answer_arr[5] || "")
                end
            end
            p question.answers.inspect
            questions << question
        end
        imported_obj = Question.import questions, recursive: true, validate: false
    end

代码插入问题但没有答案,它会给出错误提示:

NoMethodError (undefined method `answer_id=' for #<Answer:0x000000000>

我正在使用 Heroku

更新 1

CSV 样本

非常感谢任何帮助

【问题讨论】:

    标签: ruby-on-rails ruby activerecord-import


    【解决方案1】:

    如果不知道你的 csv 看起来像什么,可能很难提供更多帮助,但错误可能来自以下行:

    question.answers.build(answer: answer_arr[0], ...)
    

    该行中的后一个属性可能没问题,(我假设valuepdf_paragkeypositionnote 都是答案表上的列。如果不是这样你可能还有其他问题。)但你从answer: answer_arr[0]开始。

    它认为您正在尝试在answer 记录上设置answer(或answer_id)属性。假设 answer_arr[0] 是您要导入的数据的 id,您可以尝试将 answer: answer_arr[0] 替换为 id: answer_arr[0]

    但是,如果您要导入数据,您可能需要考虑将 id 排除在导入之外,并让 rails 为您设置新的 id。一般来说,尝试覆盖框架管理主键的方式是一个坏主意。一旦你保存了你在问题上构建的答案,Rails 就会很聪明并正确设置你的外键。如果您绝对需要保留正在导入的数据中的 id,请考虑将它们放在新列中。

    如果不正确,请提供错误跟踪并提供一些来自 csv 的示例数据。

    【讨论】:

    • "answer" 是答案表中的一列,我没有设置 ID。我刚刚更新了添加 CSV 示例的问题
    • 这一行:imported_obj = Question.import questions, recursive: true, validate: false
    • @FDI 你能解决问题吗?我确实有相同的用例。
    猜你喜欢
    • 1970-01-01
    • 2012-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多