首页 > 解决方案 > TypeError(没有将 nil 隐式转换为 Integer)Active Record

问题描述

我的 rails 应用程序目前有两个模型usertransaction

user has_many :transactions 
transaction belong_to :user


class Transaction < ApplicationRecord
  belongs_to :user
end


我可以在我的应用程序中很好地创建用户,但是当我创建事务时,它总是显示错误消息TypeError (no implicit conversion of nil into Integer),即使记录创建成功。

在此处输入图像描述

irb(main):007:0> t = Transaction.new
=> #<Transaction id: nil, user_id: nil, gateway: nil, status: nil, txn_id: ...
irb(main):008:0> t.save
Traceback (most recent call last):
        1: from (irb):8:in `<main>'
TypeError (no implicit conversion of nil into Integer)
irb(main):009:0> User.last.transactions.create
  User Load (1.2ms)  SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT $1  [["LIMIT", 1]]
  TRANSACTION (0.1ms)  BEGIN
  Transaction Create (1.0ms)  INSERT INTO "transactions" ("user_id", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id"  [["user_id", 1], ["created_at", "2021-11-05 12:24:21.163495"], ["updated_at", "2021-11-05 12:24:21.163495"]]
  TRANSACTION (0.3ms)  COMMIT
Traceback (most recent call last):
        2: from (irb):8:in `<main>'
        1: from (irb):9:in `rescue in <main>'
TypeError (no implicit conversion of nil into Integer)
irb(main):010:0> Transaction.count
   (1.8ms)  SELECT COUNT(*) FROM "transactions"
=> 2

我确信它没有与我的模型相关的问题。谁能告诉我原因?我的 gem 文件非常简单,如下所示:

source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '3.0.0'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails', branch: 'main'
gem 'rails', '~> 6.1.3', '>= 6.1.3.2'
# Use postgresql as the database for Active Record
gem 'pg', '~> 1.1'
# Use Puma as the app server
gem 'puma', '~> 5.0'
# Use SCSS for stylesheets
gem 'sass-rails', '>= 6'
# Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker
gem 'webpacker', '~> 5.0'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.7'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use Active Model has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use Active Storage variant
# gem 'image_processing', '~> 1.2'

# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.4.4', require: false

gem 'devise'

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug'
  gem 'dotenv-rails'
end

group :development do
  # Access an interactive console on exception pages or by calling 'console' anywhere in the code.
  gem 'web-console', '>= 4.1.0'
  # Display performance information such as SQL time and flame graphs for each request in your browser.
  # Can be configured to work on production as well see: https://github.com/MiniProfiler/rack-mini-profiler/blob/master/README.md
  gem 'rack-mini-profiler', '~> 2.0'
  gem 'listen', '~> 3.3'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
end

group :test do
  # Adds support for Capybara system testing and selenium driver
  gem 'capybara', '>= 3.26'
  gem 'selenium-webdriver'
  # Easy installation and use of web drivers to run system tests with browsers
  gem 'webdrivers'
end

gem 'coinpayments'

标签: ruby-on-railsactiverecord

解决方案


我发现它是由我的敏感模型属性引起的。事务模型有一个hashname 属性t.string :hash,只需更改为txn_hash,即可解决问题。


推荐阅读