ruby - Rails blog using Mongoid - Auto generate Short URL on post creation -


i have simple blog engine using rails , mongoid orm.

i have 2 models in blog, 'article' , 'url'. article model contains of post content, , url class generator function takes slug of article , creates short url it.

e.g. my-sample-blog-post -> ai3n etc. etc.

the problem having problems linking two. can't embed url class in article class either.

my question is, can generate short url on fly, post created, inside article model? article model uses mongoid::slug give me nice post slugs, need short urls each post.

any on appreciated.

i think use after create callback generate short url , store in field inside article model.

something this:

class article    field :title   slug  :title   field :short_url    after_create :generate_short_url    def generate_short_url     self.short_url = shorten_it(self.slug)   # assuming implement shorten_it      self.save   end end 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -