2011-07-18 4 views
0

私はSinatraで働いています。これは私のモデルです。Datamapperで動作しない

class Post 
    include DataMapper::Resource 
    property :id, Serial 
    property :title, String 
    property :body, Text 
    property :posted, Boolean, :default => true 

    has n, :comments 
    has n, :tags 
end 

class Comment 
    include DataMapper::Resource 
    property :id, Serial 
    property :user, String 
    property :body, Text 
    property :posted, Boolean, :default => false 

    belongs_to :post 
end 

class Tag 
    include DataMapper::Resource 
    property :id, Serial 
    property :tag, String 
    property :weight, Integer, :default => 1 

    belongs_to :post 
end 

ポスト

tags = params[:tags].split(' ') 
post = Post.new(:title=>params[:title],:body=>params[:body]) 
tags.each { |tg| 
    post.tags << Tag.create(:tag=>tg) 
} 
redirect '/admin' if post.save 

しかし、誰タグを作成します。修正する必要があるのは何ですか?あなたは1対多の関係を使用している場合

答えて

0

、あなたはpost:postセットでタグを作成する必要があります。

tags.each { |tg| 
    Tag.create(:tag => tg, :post => post) 
} 
関連する問題