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

  • SEARCH
  • Home
  • 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 6710197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:59:39+00:00 2026-05-26T07:59:39+00:00

I have a situation where I want to make a request to third-party API(url

  • 0

I have a situation where I want to make a request to third-party API(url shortening service) after creating a record in the database (updates a column in the table which stores the short url), in order to decouple the API request from the Model, I have set up an ActiveRecord Observer which kicks in every time a record is created, using after_create callback hook, here is the relevant code:

class Article < ActiveRecord::Base
  has_many :comments
end

class ArticleObserver < ActiveRecord::Observer
  def after_create(model)
    url = article_url(model)
    # Make api request...
  end
end

The problem in the above code is article_url because Rails Routes are not available in either Model or ModelObservers, same as ActionMailer (similar problem exists in Mails where if we need to put an URL we have to configure “ActionMailer::default_options_url”). In theory accessing routes/request object in Model is considered a bad design. To circumvent the above issue I could include the url_helpers module as described in the following URL:

http://slaive-prog.tumblr.com/post/7618787555/using-routes-in-your-model-in-rails-3-0-x

But this does not seem to me a clean solution, does anybody have a pointer on this issue or any advice on how it should be done?

Thanks in advance.

  • 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-26T07:59:39+00:00Added an answer on May 26, 2026 at 7:59 am

    I would definitely not let your models know about your routes. Instead, add something like attr_accessor :unshortened_url on your Article class. Set that field in your controller, and then use it from your observer. This has the added benefit of continuing to work if you later decide to set your shortened URL asynchronously via a background task.

    Edit

    A couple of things, first of all.

    1. Let’s get the knowledge of creating a short_url out of the model
      entirely.
    2. We could nitpick and say that the short_url itself doesn’t belong in the model at all, but to remain practical let’s leave it in there.

    So let’s move the trigger of this soon-to-be-background task into the controller.

    class ArticlesController < ApplicationController
      after_filter :short_url_job, :only => [:create]
    
      # ...
    
    
      protected
    
      def short_url_job
        begin
          @article.short_url = "I have a short URL"
          @article.save!
        rescue Exception => e
          # Log thy exception here
        end
      end
    end
    

    Now, obviously, this version of short_url_job is stupid, but it illustrates the point. You could trigger a DelayedJob, some sort of resque task, or whatever at this point, and your controller will carry on from here.

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

Sidebar

Related Questions

Say currently I have url like : http://mydomain.com/showpost.php?p=123 Now I want to make it
I have a situation where I want to use permissions on database objects, but
I have a situation where I want to create a signature of a data
I have a situation where I want to copy the output assembly from one
I have a situation where I want certain code to be executed no matter
I have a situation where i want to add LinePragmas to CodeDom objects. But
I have a situation where i want to return List<> from this function public
I have a situation where I want a Java client to have a two-way
I have a situation where I want to create a mapping from a tuple
I have a situation where I want to map a pair of objects to

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.