MongoMapper, Strategy Pattern and lost reference

26 views
Skip to first unread message

Rui Silva

unread,
Sep 5, 2013, 7:54:20 AM9/5/13
to mongo...@googlegroups.com
Hi!

I'm new to nosql, mongodb and mongomapper, so this might be a not so smart question. I apologize in advance for that.

What I'm trying to do is the following:
- An Account can have many numbers
- A Number can only belong to one Account
  - On the Number class I will follow the Strategy Design, thus it will have an object of type NumberStrategy.
- A Call can only belong to one Number, and a number can have many calls.

I believe that I shouldn't store NumberStrategy objects in MongoDB, since they contain no state.
What I did instead was, on Number:
 - Created a key called strategy_property, of type String. This key will contain the to_s of the current strategy object.
 - Created an instance variable called strategy, of type NumberStrategy.
 - Redefined the initialize method of to initialize the strategy attribute, according to the strategy_property passed as argument.

My problem comes when I do this:
- create Number object num
- create Call object call
- call.number = num

After this, call.number.strategy returns nil, even though num.strategy has the correct NumberStrategy object.
It seems to me that call is getting a copy of num, but the strategy attribute isn't being copied as well.

How can I fix this?

Thanks in advance.

Jamie Orchard-Hays

unread,
Sep 6, 2013, 3:25:04 PM9/6/13
to mongo...@googlegroups.com
My first question is, What does the Strategy Pattern get you here? (Why are you using the Strategy Pattern?) 

If you're using it because it seems like a good idea, drop it for now and use it when you absolutely need it.

Forgetting the SP for a moment. You can:

class Account
  include MongoMapper::Document

  many :numbers
end

class Number
  include MongoMapper::Document #(unless you want it embedded, the use MongoMapper::EmbeddedDocument)

  key :account_id, ObjectId #omit if embedded doc
  key <some_key>, <SomeType>

  belongs_to :account #omit if embeddedd
end

more info:





--
--
You received this message because you are subscribed to the Google
Groups "MongoMapper" group.
For more options, visit this group at
http://groups.google.com/group/mongomapper?hl=en?hl=en
---
You received this message because you are subscribed to the Google Groups "MongoMapper" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongomapper...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Rui Silva

unread,
Sep 6, 2013, 4:22:00 PM9/6/13
to mongo...@googlegroups.com
Hi jamieorc.

First of all, thanks for your reply.

I'm using the Strategy Pattern because on the Number class there is behaviour that I want to be able to select at runtime.

I ended up checking the memory address of both num and call.number and they are definitely two different objects. Is this normal?
I also checked if the methods self.to_mongo and self.from_mongo were being used, and they are not. Is this normal?

Thanks in advance.
Reply all
Reply to author
Forward
0 new messages