销毁“has n, :things, :through =>”资源” DataMapper 中的关联
我有一个名为 MySet: 的集合类
class MySet
include DataMapper::Resource
property :id, Serial
has n, :my_elements, :through => Resource
def add integer
unless my_elements.first(:integer => integer)
my_element = MyElement.create :integer => integer
my_elements << my_element
my_elements.save
end
self
end
def has_integer? integer
!my_elements.first(:integer => integer).nil?
end
def delete integer
if has_integer? integer
my_elements.first(:integer => integer).destroy
my_elements.save
end
self
end
def size
my_elements.size
end
end
和一个名为 MyElement: 的元素类,
class MyElement
include DataMapper::Resource
property :id, Serial
property :integer, Integer
end
我希望能够在 MySet 中添加和删除元素。但是,以下规范:
describe MySet do
subject do
MySet.create
end
it "adds and deletes" do
subject.add 1
subject.delete 1
subject.size.should == 0
end
end
失败并显示:
Failure/Error: subject.size.should == 0
expected: 0
got: 1 (using ==)
这类似于 DataMapper 通过资源 DELETE(从关联中删除)无法正常工作,除非 MyElement 未指定与 MySet 的关联。但是,我认为该帖子中的解决方案就是我正在使用的解决方案,但它似乎不起作用。
I have a collection class called MySet:
class MySet
include DataMapper::Resource
property :id, Serial
has n, :my_elements, :through => Resource
def add integer
unless my_elements.first(:integer => integer)
my_element = MyElement.create :integer => integer
my_elements << my_element
my_elements.save
end
self
end
def has_integer? integer
!my_elements.first(:integer => integer).nil?
end
def delete integer
if has_integer? integer
my_elements.first(:integer => integer).destroy
my_elements.save
end
self
end
def size
my_elements.size
end
end
and an element class called MyElement:
class MyElement
include DataMapper::Resource
property :id, Serial
property :integer, Integer
end
I want to be able to add and delete elements to and from MySet. However, the following spec:
describe MySet do
subject do
MySet.create
end
it "adds and deletes" do
subject.add 1
subject.delete 1
subject.size.should == 0
end
end
fails with:
Failure/Error: subject.size.should == 0
expected: 0
got: 1 (using ==)
This is similar to the problem described in DataMapper has n through Resource DELETE (Remove from association) not working except that MyElement does not specify an association with MySet. However, I think the solution in that post is what I am using and it does not appear to work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看集合文档:
http://rubydoc.info/github /datamapper/dm-core/master/DataMapper/Collection
http://rubydoc.info/github/datamapper/dm-core /master/DataMapper/Associations/ManyToMany/Collection
我认为您正在做的一些事情是无关紧要的,并且
MyElement.create
可能应该是MyElement.first_or_create
但更重要的是,您销毁其中一个 MyElement 的部分不太正确。看来您想要做的是将其从“my_elements”集合中删除,因此您应该找到它,然后从集合中“删除”它:(销毁它也可能是您想要的一部分做,并且您可以单独执行)。
就像您所做的那样销毁它实际上可能有效(我不确定),但您可能必须在测试中重新加载资源才能查看更新后的大小:
Take a look at the Collection documentation:
http://rubydoc.info/github/datamapper/dm-core/master/DataMapper/Collection
http://rubydoc.info/github/datamapper/dm-core/master/DataMapper/Associations/ManyToMany/Collection
I think some of the things you're doing are extraneous, and
MyElement.create
should probably beMyElement.first_or_create
but more importantly, the part where you destroy one of the MyElement's is not quite right. It seems like what you're trying to do is remove it from the "my_elements" collection, so you should find it, and then "delete" it from the collection:(destroying it may also be part of what you're trying to do, and you can do that separately).
Just destroying it like you were doing may actually work (I'm not sure), but you'd probably have to reload the resource in your test to see the updated size: