简体   繁体   English

不要在 Datamapper 中工作行为

[英]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 do I need to fix?我需要解决什么问题?

If you use one-to-many relation, you should create tags with :post set to post :如果您使用一对多关系,您应该创建标签,并将:post设置为post

tags.each { |tg|
  Tag.create(:tag => tg, :post => post)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM