ruby-on-rails - TypeError(没有将 nil 隐式转换为 Integer)Active Record
问题描述
我的 rails 应用程序目前有两个模型user
和transaction
:
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'
解决方案
我发现它是由我的敏感模型属性引起的。事务模型有一个hash
name 属性t.string :hash
,只需更改为txn_hash
,即可解决问题。
推荐阅读
- typescript - 每次在输入子项上键入时,键盘都会不断关闭
- wordcloud2 - Wordcloud2:是否可以只显示出现 n 次的单词?
- stored-procedures - 在 Google BigQuery 中重命名存储过程
- google-sheets - 索引匹配和求和值
- python - 如何从文本文件中打印字符串,在 Python 中用换行符分隔
- python - 使用单独的颜色条绘制多个 seaborn 热图
- c# - IIS App设置物理路径指向不同的文件夹位置?
- cloud - 如何获取暂存文件雪花中的列数
- button - Openlayers draw button
- excel - 如何在不崩溃文件的情况下运行大量 STOCKHISTORY 函数?也许字体,工作簿的数量,文件引用的数量?