nil:NilClass RoR的未定义方法`to_sym'

时间:2018-09-23 23:10:44

标签: ruby-on-rails postgresql

这是我第一次在堆栈溢出中发表论文,希望您能对我有所帮助。 我正在使用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

endere

这是模型

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

1 个答案:

答案 0 :(得分:1)

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

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

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

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