1

I have a has_many relationship with another object. Because of this, Mongoid creates an attribute titled item_ids as an Array so I can conveniently save numerous ids as an array. However, whenever I attempt to save a new array it never actually saves. In my rails console I run the following code.

1.9.3p385 :035 > miss.item_ids = [1,2,3]
 => [1, 2, 3] 

1.9.3p385 :036 > miss.save
[paperclip] Saving attachments.
 => true 

1.9.3p385 :037 > miss.item_ids
 => [] 

As you can see when I save the object it returns true. However when I return to check out the item_ids I'm returned an empty array. What am I doing wrong?

8
  • check the miss object messages Commented Mar 21, 2013 at 18:26
  • full_messages return an empty array. Commented Mar 21, 2013 at 18:29
  • instead of checking it in your rails console, try checking in the database console, just to make sure =) Commented Mar 21, 2013 at 18:51
  • So I should check via MongoDB? Commented Mar 21, 2013 at 18:55
  • It's just a guess but try that Commented Mar 21, 2013 at 18:56

2 Answers 2

3

'item_ids' isnt an attribute, is a method that runs some queries to return only the IDs of the relationed objects and appends them on an array.

When you do something like miss.item_ids = [1,2,3] you are basically creating that field on the document, as you are using Mongo, it will store anything for you.

Sign up to request clarification or add additional context in comments.

Comments

1

You are manually creating the relation. Try miss.items << item

Now miss.items should return an array of items

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.