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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:32:57+00:00 2026-05-26T11:32:57+00:00

Is there any built-in way to attach an instance variable to a record ?

  • 0

Is there any built-in way to attach an instance variable to a record? For example, suppose I have a User class with a foo attr_accessor:

class User < ActiveRecord::Base
  ...
  attr_accessor :foo
end

If I do

u = User.first
u.foo = "bar"
u.foo # -> "bar"
User.find(1).foo # => nil

This is the correct behavior and I understand why: the instance variable exists for the instance (in memory) and not for the record. What I want is something like a record variable, so that in the above example u.foo and User.find(1).foo return the same value within the same instance of my application. I don’t want persistence: it’s not appropriate for foo to be a column in the table, it’s just appropriate for foo to return the same value for the same record during the life cycle of e.g., a controller action, console session, etc. Nor do I want a class variable via cattr_accessor, because there’s no reason that User.find(1).foo should be the same as User.find(2).foo.

The best bet I can come up with is to fake it with a class variable array and instance methods to get/set the appropriate element of the array:

class User < ActiveRecord::Base

  cattr_accessor :all_the_foos
  self.all_the_foos = Array.new

  def foo
    self.all_the_foos[id]
  end

  def foo= new_foo
    self.all_the_foos[id] = new_foo
  end

end

This ALMOST works, but it doesn’t work with un-saved records, e.g. User.new.foo = "bar" fails.

  • 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-26T11:32:58+00:00Added an answer on May 26, 2026 at 11:32 am

    You can use Thread.current to set variables that are active within the context of a controller action invocation. Your current implementation doesn’t guarantee context reset across calls.

    class User < ActiveRecord::Base
    
      after_create    :set_thread_var
    
      def foo
        new_record? ? @foo : Thread.current["User-foo-#{id}"]
      end
    
      def foo=(val)
        new_record? ? (@foo = val) : (Thread.current["User-foo-#{id}"] = val)
      end
    
    private
    
      def set_thread_var
        Thread.current["User-foo-#{id}"] = @foo if defined?(@foo)
      end
    
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WF4 workflow service deployed in AppFabric. Is there any built-in way
In a large Application is there any way to distinguish user-defined classes with built-in
In C#, is there any built-in way to correct URLs that have parent pathing
Is there any built-in functionality for classical set operations on the java.util.Collection class? My
Is there any built-in utility or helper to parse HttpContext.Current.User.Identity.Name , e.g. domain\user to
Is there any built-in way to access the parent drive relatively on Windows? i.e,
Is there any built in way to get a users email address based on
Isn't there any built in class in PHP that has HashTable in PHP? If
Is there any built-in way to add a footer row into the WPF DataGrid
Is there any built in way to require that a block be passed 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.