销毁“has n, :things, :through =>”资源” DataMapper 中的关联

发布于 2024-12-28 23:10:39 字数 1416 浏览 2 评论 0原文

我有一个名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一指流沙 2025-01-04 23:10:39

查看集合文档:

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”集合中删除,因此您应该找到它,然后从集合中“删除”它:(

def delete integer
  element = my_elements.first(:integer => integer)
  my_elements.delete(element)
  self
end

销毁它也可能是您想要的一部分做,并且您可以单独执行)。

就像您所做的那样销毁它实际上可能有效(我不确定),但您可能必须在测试中重新加载资源才能查看更新后的大小:

subject.reload
subject.size.should == 0

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 be MyElement.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:

def delete integer
  element = my_elements.first(:integer => integer)
  my_elements.delete(element)
  self
end

(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:

subject.reload
subject.size.should == 0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文