Rake 设置 - ActiveRecord::RecordInvalid:验证失败:

问题描述

尝试运行设置 rake 任务(以填充我的数据库)。它告诉我问题必须存在。当我对“Question.count”进行计数时 - 它返回 4 - 所以我知道问题存在。

我错过了什么?错字?句法?当我查看类似的 SO 时,它显示为某种拼写错误,但我没有看到。

这是不起作用的代码

def add_option_group
  puts "  * Add Option Groups...\n"
  OptionGroup.transaction do
    create_option_group(
        option_group_name: "Always-Never",question_id: Question.find_by(question_name: "Do you have an updated photo?")
    )
    create_option_group(
        option_group_name: "Yes-No",question_id: Question.find_by(question_name: "Do you have a bio saved (updated in last 12 months)?")
    )
    end
end

def create_option_group(options={ })
  puts "  * CREATE OptionGroup Section...\n"

  option_group_attributes = {}
  attributes = option_group_attributes.merge options
  option_group = OptionGroup.create! attributes
  option_group.save!
  option_group
end

我收到此错误消息:

  • 添加选项组...
  • 创建选项组部分... 耙子中止! ActiveRecord::RecordInvalid:验证失败:问题必须存在 /Users/axxx/workspace/fresh-assess/lib/tasks/setup.rake:314:in create_option_group' /Users/axxx/workspace/fresh-assess/lib/tasks/setup.rake:298:in block in add_option_group' /Users/axxx/workspace/fresh-assess/lib/tasks/setup.rake:297:in add_option_group' /Users/axxx/workspace/fresh-assess/lib/tasks/setup.rake:51:in create_sample_data!

迁移文件

    ***** migrations ******
class CreateOptionGroups < ActiveRecord::Migration[6.1]
  def change
    create_table :option_groups do |t|
      t.bigint :question_id
      t.text :option_group_name
      t.timestamps
    end
  end
end

class CreateQuestions < ActiveRecord::Migration[6.1]
  def change
    create_table :questions do |t|
 t.bigint :assessment_section_id
 t.bigint :input_type_id
 t.text :question_name
 t.string :question_subtext
 t.boolean :question_required_yn
      t.boolean :answer_required_yn
 t.boolean :allow_multiple_options_answers_yn
 t.integer :dependent_question_id
 t.integer :dependent_question_option_id
 t.integer :dependent_answer_id
      t.timestamps
    end
  end
end

这里是架构

    t.bigint "question_id"
    t.bigint "option_choice_id"
    t.datetime "created_at",precision: 6,null: false
    t.datetime "updated_at",null: false
  end

  create_table "questions",force: :cascade do |t|
    t.bigint "assessment_section_id"
    t.bigint "input_type_id"
    t.text "question_name"
    t.string "question_subtext"
    t.boolean "question_required_yn"
    t.boolean "answer_required_yn"
    t.boolean "allow_multiple_options_answers_yn"
    t.integer "dependent_question_id"
    t.integer "dependent_question_option_id"
    t.integer "dependent_answer_id"
    t.datetime "created_at",null: false
  end

添加外键:

class ForeignMoreKeysToModels < ActiveRecord::Migration[6.1]
  def change
    add_foreign_key :option_groups,:questions,validate: false
    add_foreign_key :option_choices,:option_groups,validate: false
  end
end

在模型中:

class Question < ApplicationRecord
    has_one :assessment_section
    belongs_to :assessment_section,optional: true
    has_many :option_groups
end

class OptionGroup < ApplicationRecord
    has_many :option_choices
    belongs_to :questions
end

我错过了什么?

谢谢。

解决方法

两个问题:

  1. OptionGroup 类中,它应该说 belongs_to :question

  2. 尝试将 .id 添加到问题查找中:

create_option_group(
  option_group_name: "Always-Never",question_id: Question.find_by(question_name: "Do you have an updated photo?").id
)