【问题标题】:Sinatra + SQLite + ActiveRecord (strings can not be saved)Sinatra + SQLite + ActiveRecord(不能保存字符串)
【发布时间】:2014-12-15 06:49:06
【问题描述】:

我在数据库中保存字符串时遇到问题。整数进展顺利。我有一个模型Payment,有一个字段:command(String)。这是来自控制台:

p = Payment.new
p.command = 'test'
=> "test" 
p.save
=> D, [2014-10-20T15:30:43.383504 #2229] DEBUG -- :    (0.2ms)  begin transaction
=> D, [2014-10-20T15:30:43.386985 #2229] DEBUG -- :    (0.1ms)  commit transaction
=> true 
Payment.last
=> #<Payment id: 1, command: nil> 

我的应用文件

require 'sinatra'
require 'activerecord'
require 'sinatra/activerecord'

set :database, 'sqlite3:qiwi_exline_development.sqlite3'

get '/' do
  @payments = Payment.all
  haml :index
end

class Payment < ActiveRecord::Base
  include ActiveModel::Model

  attr_accessor :command
end

我的架构

ActiveRecord::Schema.define(version: 20141020092721) do

  create_table "payments", force: true do |t|
    t.string "command"
  end

end

有趣的时刻:如果我在模式中将字符串更改为整数 -> 整数值被保存而没有任何问题。

【问题讨论】:

  • 从模型中移除 attr_accessor。对于ActiveRecord 模型,ActiveRecord 已经为您的数据列生成了getter 和setter。不需要attr_accessorattr_accessor — 是在 ActiveRecord 模型中控制质量分配的已弃用方法。
  • 谢谢,问题很简单!请将您的评论作为答案,以便我将其标记为正确。

标签: sqlite activerecord sinatra


【解决方案1】:

从模型中删除 attr_accessor。对于ActiveRecord 模型,ActiveRecord 已经为您的数据列生成了getter 和setter。不需要attr_accessorattr_accessor — 是在 ActiveRecord 模型中控制质量分配的已弃用方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-08
    • 2015-06-23
    • 1970-01-01
    • 2012-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多