Pavel Tkackenko
Pavel Tkackenko

Reputation: 953

Sinatra + SQLite + ActiveRecord (strings can not be saved)

I have a problem with saving strings in database. Integers are going well. I have a model Paymentwith one field :command(String). This is from console:

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> 

My app file

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

My schema

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

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

end

Interesting moment: If i change string to integer in schema -> integer values are saved without any problems.

Upvotes: 2

Views: 263

Answers (1)

Roman Kiselenko
Roman Kiselenko

Reputation: 44360

Remove attr_accessor from model. In the case of ActiveRecord models, getters and setters are already generated by ActiveRecord for your data columns. attr_accessor is not needed. attr_accessor — is a deprecated method of controlling mass assignment within ActiveRecord models.

Upvotes: 1

Related Questions