私のアプリケーションに新しいオブジェクトを作成する際に問題があります。どうやら、データはSequel :: Modelとdbテーブル自体の間でどこかで失われてしまい、idとすべての属性がNULLのエントリになります。Ruby Sequel SQLite3アプリケーションはNULL属性のデータベースに有効なオブジェクトを保存します
これは私のモデルである:
class Wallet < Sequel::Model(:wallets)
attr_accessor :address, :balance
def validate
super
errors.add(:address, "Address can't be empty") if address.empty?
errors.add(:address, "Input address already exists in the db") unless Wallet.where(address: address).empty?
end
end
そしてこれは、テーブルの作成、移行です:
Sequel.migration do
change do
create_table(:wallets) do
primary_key :id, unique: true
String :address
Integer :balance
end
end
end
私はrodaフレームワークを使用しています。ここウォレットオブジェクトが作成されるwallet_app.rb、次のとおりです。上記のクラスでのコメントで指摘したように
require 'roda'
require 'sequel'
require 'json'
DB = Sequel.connect('sqlite://database.sqlite3')
class WalletApp < Roda
require './lib/services/balance_service'
require './models/account'
route do |r|
...
r.post "wallets" do
address = r.params["address"]
balance = BalanceService.get_balance(address)
wallet = Wallet.new(address: address, balance: balance)
# Until here we have the attributes correctly set on wallet object
if wallet.valid? && wallet.save
# Now wallet is persisted in the db with an id but address and balance NULL
wallet.to_json
else
wallet.errors.to_json
end
end
end
end
は、オブジェクトは、DBに挿入する前に有効であり、属性が正しく設定され表示されます。それでも、データはすべての属性がNULLであるため永続化されます。私は、マイグレーションやモデル定義のエラーを想定していますが、見つけられませんでした。それが助け場合
、私はここにも私のGemfileをコピーします。
source "https://rubygems.org"
ruby '2.1.2'
gem 'roda'
gem 'sequel'
gem 'sqlite3'
gem 'httparty'
感謝を事前に
これでした。ありがとう@ジェレミー – ntonnelier