2011-12-01 124 views
0

我試圖保存到sqlite 3數據庫。我似乎無法保存,但沒有發生錯誤。我究竟做錯了什麼?我從現有的一段代碼中取出了保存程序。關於下一步我應該嘗試的任何想法?使用DataMapper保存數據

require 'data_mapper' 
require 'dm-migrations' 

DataMapper.setup :default, "sqlite://#{Dir.pwd}/development.sqlite3" 

class Post 
    include DataMapper::Resource 
    property :title,  String, :key => true # An auto-increment integer key 
    property :slug_url, String 
    property :desc,  String 
    property :content, String 
    property :project, String 
    property :target_url, String 
    property :trackback, Boolean 
    property :updated_at, String 
    property :created_at, DateTime # A DateTime, for any date you might like. 
end 

DataMapper.finalize 
DataMapper.auto_upgrade! 

post = Post.new 
post.attributes = {:title => "Title", 
       :slug_url => "Some-slug-url", 
       :content => "content", 
       :target_url => "http://example.com", 
       :trackback => false 
} 
post.save 

回答

0

我不知道爲什麼這個工程有什麼不同,但我得到了它做這個工作:

require 'data_mapper' 
require 'dm-migrations' 

DataMapper.setup :default, "sqlite://#{Dir.pwd}/development.sqlite3" 

class Post 
    include DataMapper::Resource 
    property :title,  String, :key => true # An auto-increment integer key 
    property :slug_url, String # A varchar type string, for short strings 
    property :desc,  String 
    property :content, String 
    property :project, String 
    property :target_url, String 
    property :trackback, Boolean 
    property :updated_at, DateTime 
    property :created_at, DateTime # A DateTime, for any date you might like. 
end 

DataMapper.finalize 
DataMapper.auto_upgrade! 

post = Post.new(:title => "Title", 
        :slug_url => "Some-slug-url", 
        :content => "content", 
        :target_url => "http://example.com", 
        :trackback => false) 
post.save 
+1

property:updated_at,String應該是DateTime –

+0

@AndrewKolesnikov修正! –

0

「無錯誤拋出」部分可以固定DataMapper::Model.raise_on_save_failure = trueDataMapper不默認保釋。 #save!是不安全的版本(不知道爲什麼ActiveRecord使用與爆炸完全不同的語義)。 For further information, read here.

拋出的第一個錯誤是column title is not unique (DataObjects::IntegrityError)。猜猜發生了什麼;-)