Reputation: 1058
My rails application currently has two models user
and transaction
:
user has_many :transactions
transaction belong_to :user
class Transaction < ApplicationRecord
belongs_to :user
end
I can create user well in my app, but when I create transaction, it always show the error message TypeError (no implicit conversion of nil into Integer)
, even though the record is created successfully.
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
I'm sure that it has no issue related to my model. Could anyone let me know the reason? My gem file is really simple as below:
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'
Upvotes: 9
Views: 2259
Reputation: 1058
I found that it is caused by my sensitive model attribute.
The transaction model has a hash
name attribute t.string :hash
, just change to txn_hash
, it fixes the issue.
Upvotes: 22