Mongo ids leads to scary URLs

前端 未结 4 1248
时光取名叫无心
时光取名叫无心 2020-12-24 01:52

This might sound like a trivial question, but it is rather important for consumer facing apps

What is the easiest way and most scalable way to map the scary mongo id

4条回答
  •  爱一瞬间的悲伤
    2020-12-24 02:33

    You can create a composite key in mongoid to replace the default id using the key macro:

    class Person
      include Mongoid::Document
      field :first_name
      field :last_name
      key :first_name, :last_name
    end
    
    person = Person.new(:first_name => "Syd", :last_name => "Vicious")
    person.id # returns "syd-vicious"
    

    If you don't like this way to do it, check this gem: https://github.com/hakanensari/mongoid-slug

提交回复
热议问题