开发者

Don't work behaviors in Datamapper

开发者 https://www.devze.com 2023-03-20 20:09 出处:网络
I work with Sinatra. This is my models. class Post include DataMapper::Resource property :id, Serial property :title, String

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)
}
0

精彩评论

暂无评论...
验证码 换一张
取 消