Sorting an associated EmbeddedDocument

15 views
Skip to first unread message

overture

unread,
Jan 18, 2010, 8:37:56 AM1/18/10
to MongoMapper
Hi,

How can you sort on an assoicated EmbeddedDocument? For instance, if
I have a "Post" Document and a "Comment" EmbeddedDocument - how can I
sort the comments by "created_at".

I tried...

>> post = Post.first
>> post.comments(:order => "created_at")

...but it didn't seem to work. I'm sure I'm doing something stupid
but I can't work it out!

Thanks!
Phil

Brandon Martin

unread,
Jan 18, 2010, 8:45:42 AM1/18/10
to mongo...@googlegroups.com
Do you have

timestamps!

set in your model? If not there is no created_at column.

Brandon

overture

unread,
Jan 18, 2010, 9:18:59 AM1/18/10
to MongoMapper
Yeah, I have a created_at column.

On Jan 18, 1:45 pm, Brandon Martin <bmar...@zyphmartin.com> wrote:
> Do you have
>
> timestamps!
>
> set in your model? If not there is no created_at column.
>
> Brandon

> <http://www.zyphmartin.com>

Kyle Banker

unread,
Jan 18, 2010, 9:24:42 AM1/18/10
to mongo...@googlegroups.com
You many find this thread of value:

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

Gustavo Beathyate

unread,
Jan 18, 2010, 9:22:18 AM1/18/10
to mongo...@googlegroups.com
I think you could do:

post.comments.sort_by(&:created_at)

And get the same result. But I don't know if that would be the best
way to do it.

overture

unread,
Jan 18, 2010, 2:35:26 PM1/18/10
to MongoMapper
Excellent! Thanks Gustavo, that seems like a really nice way to do
it. Also, to get descending order use:

post.comments.sort_by(&:created_at).reverse

Thanks!

On Jan 18, 2:22 pm, Gustavo Beathyate <r...@obviamente.pe> wrote:
> I think you could do:
>
> post.comments.sort_by(&:created_at)
>
> And get the same result. But I don't know if that would be the best
> way to do it.
>

Reply all
Reply to author
Forward
0 new messages