Don't work behaviors in Datamapper
I work with Sinatra. This is my models.
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
Create a post
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
But no tags. What开发者_JS百科 do I need to fix?
If you use one-to-many relation, you should create tags with :post
set to post
:
tags.each { |tg|
Tag.create(:tag => tg, :post => post)
}
精彩评论