Is there a way for mongoid to use an integer (number) as the default value, rather than a long hash value?

I just want to have a default ActiveRecord feature that uses incremental integers as id to reduce the length of the URL.

For example, the first article you create will show the URL "app.com/articles/1", which is used by default in ActiveRecord.

Is there any stone that supports this in the mangooid?

+7
source share
3 answers

You can always generate shorter unique markers to identify each of your entries (as an alternative to punching), since your goal is simply to reduce the length of the URL.

I recently (today) wrote a gem - mongoid_token , which should do any hard work to create unique tokens for your manga documents. It will not generate them sequentially, but it should help you with your problem (I hope!).

+6
source

You can try something like this:

class Article include Mongoid::Document identity :type => Integer before_create :assign_id def to_param id.to_s end private def assign_id self.id = Sequence.generate_id(:article) end end class Sequence include Mongoid::Document field :object field :last_id, type => Integer def self.generate_id(object) @seq=where(:object => object).first || create(:object => object) @seq.inc(:last_id,1) end end 

I have not tried this approach (using it with internal identifiers), but I'm sure it should work. Check out my app here: https://github.com/daekrist/Mongologue I have added a "visible" identifier called pid to my posts and comments. I also use a text identifier for the Tag model.

+3
source

AFAIK this is not possible by design: http://groups.google.com/group/mongoid/browse_thread/thread/b4edab1801ac75be

So the community approach is to use a bullet: https://github.com/crowdint/slugoid

+2
source

All Articles