【问题标题】:Rake Setup - ActiveRecord::RecordInvalid: Validation failed:Rake 设置 - ActiveRecord::RecordInvalid:验证失败:
【发布时间】:2021-07-08 20:06:00
【问题描述】:

在尝试运行设置 rake 任务(以填充我的 dbase)。它告诉我问题必须存在。当我对“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", precision: 6, 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", precision: 6, null: false
    t.datetime "updated_at", precision: 6, 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

我错过了什么?

谢谢。

【问题讨论】:

    标签: activerecord ruby-on-rails-6 rake-task


    【解决方案1】:

    两个问题:

    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
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多