【发布时间】:2010-12-21 09:40:06
【问题描述】:
我有这两个课,
class User
include DataMapper::Resource
property :id, Serial
property :name, String
has n :posts, :through => Resource
end
class Post
include DataMapper::Resource
property :id, Serial
property :title, String
property :body, Text
has n :users, :through => Resource
end
所以一旦我有一个新帖子,比如:
Post.new(:title => "Hello World", :body = "Hi there").save
我在关联中添加和删除时遇到严重问题,例如:
User.first.posts << Post.first #why do I have to save this as oppose from AR?
(User.first.posts << Post.first).save #this just works if saving the insertion later
我应该如何从该关联中删除帖子? 我正在使用以下内容,但绝对无法正常工作:
User.first.posts.delete(Post.first) #returns the Post.first, but nothing happens
User.first.posts.delete(Post.first).save #returns true, but nothing happens
User.first.posts.delete(Post.first).destroy #destroy the Post.first, not the association
所以我真的不知道如何从 BoltUser 数组中删除它。
【问题讨论】:
标签: ruby-on-rails ruby datamapper has-and-belongs-to-many