我正在跟隨克隆互聯網應用與Ruby的tinyclone示例,並試圖創建一個對象存儲到MySQL。DataMapper無法保存到MySQL並創建對象與ID =零
但是,每當我嘗試創建Url對象(請參閱下面的代碼)時,結果始終爲 「=> Url @ id = nil @ original =」http://www.gmail.com「@link_identifier =無「
該id不會創建,也不會將數據存儲在數據庫中。到sql數據庫的鏈接是正確的,因爲我已經嘗試刪除數據庫並使用DataMapper.auto_migrate重新創建它!
任何人都可以幫忙嗎?謝謝。
DataMapper.setup(:default,'mysql://[email protected]/tinyclone')
class Url
include DataMapper::Resource
property :id, Serial
property :original, String, :length => 255
belongs_to :link
end
class Link
include DataMapper::Resource
property :identifier, String, :key => true
property :created_at, DateTime
has 1, :url
has n, :visits
end
DataMapper.finalize
url = Url.create(:original => 'http://www.gmail.com')
=> #<Url @id=nil @original="http://www.gmail.com" @link_identifier=nil>
顯然,我需要首先創建鏈接和link_identifier。 – revolver