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 6794397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:13:02+00:00 2026-05-26T18:13:02+00:00

Active Resource can make use of HTTP authentication set at the class level. For

  • 0

Active Resource can make use of HTTP authentication set at the class level. For instance:

class Resource < ActiveResource::Base
end

Resource.user = 'user'
Resource.password = 'password'

or

Resource.site = "http://user:password@site.com/"

But what if I use different HTTP authentication based on which user is logged in? If I change Resource.user and Resource.password, is that going to cause a race condition where requests from one thread suddenly start using the authentication of a user whose requests are running simultaneously in a different thread? Or is this a non-issue (as long as I reset the authentication between requests) because rails servers are not multithreaded?

Even if there’s no thread safety problem, it still seems risky that if I fail to reset them, the previous user’s credentials will be used automatically by future requests.

Update: After being frustrated with ActiveResource, I wrote my own REST library:
https://github.com/DeepWebTechnologies/well_rested

  • 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-26T18:13:02+00:00Added an answer on May 26, 2026 at 6:13 pm

    Monkey patch the host, user and password methods of ActiveResource::Base class:

    class ActiveResource::Base
      # store the attribute value in a thread local variable
      class << self
        %w(host user password).each do |attr|               
    
          define_method(attr) do
            Thread.current["active_resource.#{attr}"]
          end
    
          define_method("#{attr}=") do |val|
            Thread.current["active_resource.#{attr}"] = val
          end
        end
      end
    end
    

    Now set the credentials in every request

    class ApplicationController < ActionController::Base
    
      around_filter :set_api_credentials
    
    private 
    
      # set the credentials in every request
      def set_api_credentials
        ActiveResource::Base.host, 
          ActiveResource::Base.user, 
            ActiveResource::Base.password = current_user_credentials
        yield
      ensure
        ActiveResource::Base.host = 
          ActiveResource::Base.user = 
            ActiveResource::Base.password = nil
      end
    
      DEFAULT_HOST, DEFAULT_USER, DEFAULT_PASSWORD= [
        "http://www.foo.com", "user1", "user78102" ]
    
      def current_user_credentials
        current_user.present? ? 
          [ current_user.host, current_user.login, current_user.password] : 
          [ DEFAULT_HOST, DEFAULT_USER, DEFAULT_PASSWORD]
      end
    
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can't find the place where Active Resource initiates its connections. I was expecting
How to use cryptography with XML documents used for rails Active resources? Or, is
I'm having the following many to many relationship in Rails (ActiveResource, of course): class
Castle Active Record? Plain NHibernate? Subsonic? Entity? Something like Nettiers? I'd like to use
So I come from a background in Java where you can create a class
Rails ActiveResource is awesome ... except for one thing: as far as I can
I have two RoR3 application: http://users.domain.local http://profiles.domain.local I created the 'users/models/profile.rb': class Profile <
Question Summary: how can I get the path to the currently active application under
Is it possible to receive objects of different Active Resource models in a single
I'm trying to set up a MySQL trigger, but I can't figure out how

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.