[mongoid] pretty urls

746 views
Skip to first unread message

Ned

unread,
May 10, 2010, 9:28:42 AM5/10/10
to Mongoid
I have a feeling this may go against the grain of MongoDB

But is there a way to make urls like this prettier?

http://localhost:3000/blogs/4be804d9146ea60e1d000005

Am ok with IDs being in the URL, but do they need to be so long? How
about this?:

http://localhost:3000/blogs/4be8-Example-blog-post

And are incremental decimal IDs totally out of the question?

cheers

Kurt Mackey

unread,
May 10, 2010, 9:32:43 AM5/10/10
to mon...@googlegroups.com
There's no good way to do any kind of incrementing number using Mongo.  Your best bet is to generate a "slug" for each object, ensure that it's unique in a before_save, and then use that the generate your urls (add a to_param method that returns the slug).

For things like blog posts, that makes a good deal of sense.  Your slug should (in theory) change and match the title at any given time, while the ID shouldn't.

You can also just generate your own IDs, but I'd really only use that if the object you're dealing with has a natural ID attribute already.

-Kurt

Nickolay Kolev

unread,
May 10, 2010, 12:52:27 PM5/10/10
to mon...@googlegroups.com
> Your
> best bet is to generate a "slug" for each object, ensure that it's unique in
> a before_save, and then use that the generate your urls (add a to_param
> method that returns the slug).
> For things like blog posts, that makes a good deal of sense.  Your slug
> should (in theory) change and match the title at any given time, while the
> ID shouldn't.

Isn't this what the "key" method is for?

Rodrigo Alvarez

unread,
May 10, 2010, 2:17:21 PM5/10/10
to mon...@googlegroups.com
right now I am using key() to handle that.

Kurt Mackey

unread,
May 10, 2010, 3:02:54 PM5/10/10
to mon...@googlegroups.com
Well sure, that's how you'd use another field as an ID.  But you still don't want your IDs changing if you can help it.

Rodrigo Alvarez

unread,
May 10, 2010, 3:14:50 PM5/10/10
to mon...@googlegroups.com
On Mon, May 10, 2010 at 9:02 PM, Kurt Mackey <ku...@mubble.net> wrote:
Well sure, that's how you'd use another field as an ID.  But you still don't want your IDs changing if you can help it.

It depends on your application. Maybe that field is the perfect ID field, mainly because it is the unique identifier of a resource.

Kristian Mandrup

unread,
Nov 30, 2012, 8:47:02 AM11/30/12
to mon...@googlegroups.com, pap...@gmail.com
Try with the stringex gem ;)


I just added a Mongoid adapter. I hope my pull request to the gem owner will be merged in shortly.
Chers!

Andreas Saebjoernsen

unread,
Nov 30, 2012, 11:21:43 AM11/30/12
to mon...@googlegroups.com
The mongoid slug gem solves the slugging problem:
- https://github.com/digitalplaywright/mongoid-slug

By default slugs are generated using stringex, and Model.find is
clever enough to know when to find by slug or when to find by id.

I am maintaining this gem and there are a lot of details about
slugging that are non-trivial if you want to solve the problem well.
If the slug is permanent and you do not use things like CanCan
load_and_authorize_resource a simpler solution will probably work
well. However, it is not advisable to replace the bson id field with a
user-defined slug.

Daniel Doubrovkine

unread,
Nov 30, 2012, 4:42:37 PM11/30/12
to mon...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages