Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8050693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:08:20+00:00 2026-06-05T07:08:20+00:00

I have a collection class called MySet: class MySet include DataMapper::Resource property :id, Serial

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T07:08:21+00:00Added an answer on June 5, 2026 at 7:08 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class called LoanApplication, and it has a collection property set up
I have a class called SynonymMapping which has a collection of values mapped as
I have a class called Continent and Country. Continent class has Country collection: private
I have an object that has a child collection: class Person { public string
I have a class called WorkingDays, this class requires a collection of dates that
I have an class which has a collection, mapped as a bag in my
I have a class called Entry. This class as a collection of strings called
Assume I have a class called MyClass that has two properties (int Id and
I have the following code in a collection: class Author(Agent): def foo(self): self.find_another_document_and_update_it(ids) self.processed
I have class called GroupSelect and made a collection List(Of GroupSelect)() . Now I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.