Datamapper,SQLite,不保存数据库的属性

时间:2014-05-12 23:42:21

标签: ruby sqlite datamapper

像这样定义Datamapper类:

require 'rubygems'
require 'data_mapper'
require 'dm-core'

DataMapper::Logger.new($stdout, :debug)
DataMapper.setup(:default, ENV['DATABASE_URL'] || "sqlite3://#{Dir.pwd}/db/eventdb.sqlite")

class Temp 
  include DataMapper::Resource
  Event.raise_on_save_failure = true

  property :id, Serial
  property :temp1, String, :length => 50
  property :temp2, String
  property :temp3, String
  property :created_at, Time
  property :updated_at, Time

  attr_accessor :temp1, :temp2, :temp3

  class << self

  end

end

使用以下语法在SQLite中创建表:

〜(0.024280)CREATE TABLE&#34; temps&#34; (&#34; id&#34; INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,&#34; temp1&#34; VARCHAR(50),&#34; temp2&#34; VARCHAR(50),&#34; temp3&#34; VARCHAR(50),&#34; created_at&#34; TIMESTAMP,&#34; updated_at&#34; TIMESTAMP)

然而,当我运行此代码时,它会生成一个带有空值的insert语句,而不是时间戳条目:

irb(main):001:0> @t = Temp.new
=> #<Temp @id=nil @temp1=nil @temp2=nil @temp3=nil @created_at=nil @updated_at=nil>
irb(main):002:0> @t.temp1 = "Test1"
=> "Test1"
irb(main):003:0> @t.dirty?
=> true
irb(main):004:0> @t.save
 ~ (0.002224) INSERT INTO "temps" ("created_at", "updated_at") VALUES ('2014-05 12T19:26:15.640051-04:00', '2014-05-12T19:26:15.639557-04:00')
=> true

为什么我的属性,在这种情况下temp1没有保存到数据库?提前谢谢。

1 个答案:

答案 0 :(得分:0)

删除此行:attr_accessor :temp1, :temp2, :temp3

它可能重新定义了Datamapper的setter。