Custom Serialization

8 views
Skip to first unread message

Raving Genius

unread,
Aug 3, 2010, 10:12:30 PM8/3/10
to MongoMapper
I have what I am assuming is a basic example of custom serialization
(to_mongo, from_mongo), but it is not working. I have placed all
relevant code into a gist: http://gist.github.com/507531. Please help!

Fabrizio Regini

unread,
Aug 4, 2010, 5:52:19 AM8/4/10
to mongo...@googlegroups.com
Hi,
Array key does not automatically cast objects back (from_mongo). It only does to_mongo serialization. You have to create a custom type, maybe extending Array.

Assuming your filter takes an hash as init param, you can do this.

class FilterArray < Array
def self.from_mongo(value)
value.map do |item|
Filter.new(item)
end
end
end

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

Toni Tuominen

unread,
Aug 4, 2010, 6:19:19 AM8/4/10
to mongo...@googlegroups.com
Dunno if it actually has any effect in your case but you have a typo
in the key definition:

key :filters, Array, :typcast => 'Filter'

Other than that I don't see any reason why it shouldn't work. This is
what I do with Money attributes:
http://github.com/tjtuom/mm-money/blob/master/lib/mm-money.rb. I have
those ifs there cause for some reason the methods get called multiple
times and stuff that has been returned by to_mongo get passed again to
to_mongo. Also remember you might get nil passed into both methods.

- Toni

Fabrizio Regini

unread,
Aug 4, 2010, 6:35:19 AM8/4/10
to mongo...@googlegroups.com
Can you please provide the code of the model where you use Money class? Do you use that to typecast an Array key?
Looking at the code in master branch, I'm pretty sure the :typecast option gets evaluated only on 'set'.

Toni Tuominen

unread,
Aug 4, 2010, 7:16:29 AM8/4/10
to mongo...@googlegroups.com
No I'm using just as a key (key :price, Money).

http://github.com/jnunemaker/mongomapper/blob/master/examples/keys.rb
I'm assuming that since John has put the typecast on an Array key in
the examples it is a valid approach.

- Toni

Raving Genius

unread,
Aug 4, 2010, 1:35:05 PM8/4/10
to MongoMapper
Thanks for catching that. :typecast had a typo and that was the issue.

Fabrizio Regini

unread,
Aug 4, 2010, 4:03:50 PM8/4/10
to mongo...@googlegroups.com
I spent some time yesterday on a similar case, and I'm quite sure you cannot get an array of instances of Filter from mongo this way. You'll probably get an array of strings.
Can you confirm please?
Thannks,

Raving Genius

unread,
Aug 5, 2010, 1:38:28 PM8/5/10
to MongoMapper
From what I can tell, you are correct. I updated the gist to
illustrate how I handle the situation. Basically I am
calling :from_mongo manually. This is unfortunate, as the examples I
found online seem to indicate that :from_mongo will be called
automatically by MongoMapper if it exists. At any rate it works
correctly now.

John Nunemaker

unread,
Aug 5, 2010, 5:05:49 PM8/5/10
to mongo...@googlegroups.com
Ah, typecast only calls to_mongo when saving it. It does not currently do from mongo when pulling from Mongo. I would make a type that is FilterArray. Then do key :filters, FilterArray which defines to and from mongo. Make sense?

Raving Genius

unread,
Aug 7, 2010, 11:58:33 AM8/7/10
to MongoMapper
I think I've got it. I've updated my gist to reflect your suggestion.
On a side note, where does filter_array.rb belong? I placed it in app/
models because it is a class only used by an existing model. Does it
matter where it goes?

Thanks for you help.

John Nunemaker

unread,
Aug 7, 2010, 3:22:14 PM8/7/10
to mongo...@googlegroups.com
I would put it in app/plugins or app/types or lib/types. It is really up to you. If you put it in app/ with a non standard folder, you'll have to add the folder to the load path in environment.rb (see guides.rubyonrails.org for info on this).

Reply all
Reply to author
Forward
0 new messages