Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4593362
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T22:40:23+00:00 2026-05-21T22:40:23+00:00

My rails app has a simple model Links which tracks all the Urls associated

  • 0

My rails app has a simple model Links which tracks all the Urls associated with an Update.

class Link
include Mongoid::Document
include Mongoid::Timestamps

field :url, type: String
field :domain, type: String
attr_accessible :url, :domain

validates_uniqueness_of :url

has_and_belongs_to_many :updates

before_create :longenate

But before I create the object I’d like to get rid of any url shortening, for various reasons like longevity of link, and overall just not liking shorteners.

Currently my longenate function is very naive (still deving, so its supposed to be 🙂 ), and creates quite a bottleneck since it blocks during the request, which can be quite a while if quite a few updates are being created at a time, and usually its a few dozen being created. As the Updates/Links and so forth need to be displayed back to the user quickly, this is a problem.

    http_client = HTTPClient.new(:agent_name => "Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 6.0)")
begin
    head_res = http_client.head(url)
    unless head_res.header['Location'].empty?
        short_url = url
        self.url = head_res.header['Location'][0]    
    end
rescue Exception => e
    self.domain = "ERROR ACCESSING URI"
    self.url = "Error Accessing URI, site down/non existant?"
end

I would like to swap this out for something non blocking. I am familiar with eventmachine, and have used it to create some simple services and consumers, but am still relatively new to rails. What would be the best rails approach for something like this? A “finalize” function that includes an event machine loop to lengthen/create all the Links at one time, or maybe doing (somehow) within the model so that it spins off an async task whenever one is created?

Thanks for any advice/resources

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T22:40:23+00:00Added an answer on May 21, 2026 at 10:40 pm

    There are a couple of background processing frameworks (delayedjob, stalker, resque, etc)…. Arguably the easiest to get started with with Delayed Job.

    Once you have delayedjob installed (gem install plus a migration), then you can do things like:

    class Link
      ..
      before_create :longenate
      def longenate
        # do your stuff
        self.save
      end
      handle_asychronously :longenate
    end
    

    Then you have workers do their work using rake jobs:work. This will take place in the background; with this implementation, you’ll be saving records that have not been longenate’d, so you may want a ‘status’ field that tacks if it’s been longenated or not.

    More info: http://www.therailsway.com/2009/7/22/do-it-later-with-delayed-job

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My Rails 3 app has a User model and a Profile model. A User
My Rails-app has a sign in box with a remember me checkbox. Users who
I have a rails app that has picked up a bit of traction, but
I'm working on a rails app that has a whole bunch of before filters
I need to store app specific configuration in rails. But it has to be:
My Rails app has complicated rules about when a bit of content should be
Total noob question. I am building a simple photoblog in Rails which consists of
Our rails app is designed as a single code base linking to multiple client
In my rails app I use the validation helpers in my active record objects
On my rails app I have a list of items (like a task list)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.