aressidi
aressidi

Reputation: 680

Deleting an item from a Mongo array using Mongoid and Rails

I have an embedded document that that has an array of contributors. Contributors is an array of user ID's of people who have contributed to the document.

field :contributors, type: Array, :default => []

Here's an example item from the console:

#<Item _id: 5249d5bd06387b91a600000f, name: "Collapse", contributors: ["51db6d58bd02861e96000004", "51db6d58bd02861e96000004"], count: 2>

I want to be able to remove the first matching contributor in an item from the array, but every time I try and test just to see if the user's id is present in contributors array, it returns false when clearly it is there.

Here's an example:

contributors.include?("51db6d58bd02861e96000004")
 => false 

How do i work with array values from Mongoid? Why is this returning false?

Upvotes: 0

Views: 514

Answers (1)

Enrique Fueyo
Enrique Fueyo

Reputation: 3488

contributor is an Array of Strings or ObjectIds?

Try instead:

contributors.include?( Moped::BSON::ObjectId.from_string "51db6d58bd02861e96000004")

I don't really know your whole application. but have you considered this approach?

class Item
 has_and_belongs_to_many :users, foreign_key: :contributors
end

Upvotes: 2

Related Questions