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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:12:07+00:00 2026-05-27T09:12:07+00:00

I’ve moved an application to using ActiveResource and I’m finding that I need to

  • 0

I’ve moved an application to using ActiveResource and I’m finding that I need to rethink the way I’ve taught myself to do some things, and I’m searching for the best way to go about this.

For example, I need to keep a query handy say @current_account which I’ve done as

@current_account ||= Account.where(etc etc)

in an applicationcontroller.rb for a certain scope. This isn’t all that useful with AR, because the call to the API is made each time. I’d like to minimize calls to the api (especially where I have other more expensive calls I don’t want run on every query, I want to run them once and keep them handy)

So, what is the Rails way? I have to keep a variable with an AR call to an API handy from the ApplicationController in a certain scope, across several other controllers without having to write it out each time (or call the API each time, or put it in a user accesible session because it isn’t exactly text/strings, it is objects I need to use).

I’m curious about how others do this, if I should or should not be doing this, what is the right DRY way, etc. So this is somewhat open-ended.

Any input appreciated.

  • 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-27T09:12:08+00:00Added an answer on May 27, 2026 at 9:12 am

    It’s best to create a module for this kind of behavior:

    module CustomAuth
      def self.included(controller)
        controller.send :helper_method, :current_account, :logged_in?
      end
    
      def current_account
        # note the Rails.cache.fetch. First time, it will
        # make a query, but it caches the result and not
        # run the query a second time.
        @current_account ||= Rails.cache.fetch(session[:account_id], Account.where(...))
      end
    
      def logged_in?
        !current_account.nil?
      end
    end
    

    Then, make sure that Rails loads up this file (I put mine in ./lib/custom_auth.rb), so add that to the config.autoload_paths in ./config/application.rb:

    # ./config/application.rb
    ...
    config.autoload_path += %W(#{config.root}/lib)
    ...
    

    Import the CustomAuth module into your application_controller.rb:

    class ApplicationController < ActionController::Base
      include CustomAuth
      protect_from_forgery
    
      ...
    end
    

    Finally, Crucial: Restart your server

    NOTE: You can add additional methods to the custom_auth.rb. If you restart the server, they will be available. These methods are also available in the view, so you can call current_account.name right inside a view.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I have thousands of HTML files to process using Groovy/Java and I need to
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into

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.