0
有DataMapper的類定義是這樣的:DataMapper的,SQLite的,不節約性數據庫
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 「臨時工」 (「id」INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,「temp1」VARCHAR(50),「temp2」VARCHAR(50),「temp3」VARCHAR(50),「created_at」TIMESTAMP,「updated_at」TIMESTAMP)
當我運行這段代碼時,它會生成一個插入語句,其中包含時間戳記條目以外的空值:
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不保存到數據庫?提前致謝。
謝謝,這樣做!以爲我需要attr_accessor來使屬性可讀/寫。再次感謝。 – jmtdev
如果我的答案解決了您的問題,您應該用答案左側的綠色勾號接受它。 – ujifgc