首页 > 解决方案 > 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

标签: ruby-on-railspostgresql

解决方案


您生成了没有主键的竞争者表。结帐此行:

create_table "competitors", id: false, force: :cascade do |t|

是你的id: false问题。检查迁移以创建competitors表并设置主键(或创建一个新的迁移添加主键)。

有用的资源:https ://stackoverflow.com/a/10079409/740394


推荐阅读