ruby-on-rails - nil:NilClass RoR 的未定义方法“to_sym”
问题描述
这是我在堆栈溢出方面的第一篇文章,希望你能帮助我。我正在使用 RoR 和 PostgreSQL,gem 'devise'。
在 Rails 控制台中,我试图从“竞争对手”表中删除数据,但出现以下错误并且无法解决。
2.4.1 :006 > c.destroy(c) ActiveRecord::UnknownPrimaryKey: 模型竞争对手中表竞争对手的未知主键。
This is my competitors table, which it's was generate with model of gem devise
create_table "competitors", id: false, force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.bigint "rut"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.inet "current_sign_in_ip"
t.inet "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "name"
t.string "lastname"
t.integer "phone"
t.date "dateOfBirth"
t.boolean "gender"
t.string "numberSerie"
t.string "otp_secret_key"
t.integer "otp_module", default: 0
t.index ["email"], name: "index_competitors_on_email", unique: true
t.index ["reset_password_token"], name: "index_competitors_on_reset_password_token", unique: true
t.index ["rut"], name: "index_competitors_on_rut", unique: true
安德烈
这就是模型
class Competitor < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :raffle_registers, primary_key: 'rut', foreign_key: 'rutCompetitors'
has_many :accountpays
has_one :found
#otp model to make it use TFA
has_one_time_password
enum otp_module: { disabled: 0, enabled: 1 }, _prefix: true
attr_accessor :otp_code_token
end
解决方案
您生成了没有主键的竞争者表。结帐此行:
create_table "competitors", id: false, force: :cascade do |t|
是你的id: false
问题。检查迁移以创建competitors
表并设置主键(或创建一个新的迁移添加主键)。
推荐阅读
- python - 从python中的路径创建超链接
- ios - 有条件地启动适用于 iOS <14 的 WKWebView 或适用于 iOS 14+ 的 SwiftUI App?
- sql - oracle中字符串中第n次出现子字符串后的子字符串
- mysql - 在 PDO MySQL 中是否可以通过组数来限制,而不是总行数?
- java - 尝试使用 Java 11、OpenJSK、Apache Tomcat/8.5.41 配置 Websocket 端点
- java - 如何从 getIntent 将第一个值设置为 EditActivity 的视图模型
- android - Android Listview 项目隐藏在应用栏下
- ios - iOS中子视图内的像素化文本
- python - 使用 while 循环创建函数
- visual-studio-code - 如何修复高对比度主题 VS 代码中的暗文件名