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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T04:44:01+00:00 2026-05-21T04:44:01+00:00

In an ActiveRecord::Base model, I can reset the state of the model to what

  • 0

In an ActiveRecord::Base model, I can reset the state of the model to what it was when I got it from the database with reload, as long as the attribute I’m setting maps to a table column:

user = User.first
user.email #=> "email@domain.com"
user.email = "example@site.com"
user.email #=> "example@site.com"
user.reload
user.email #=> "email@domain.com"

But if I add a custom attribute, the only way I’ve found to have it act the same is like this:

class User < ActiveRecord::Base
  attr_accessor :user_agent

  def reload
    super
    self.user_agent = nil
    self
  end
end

My question is, is there some API to make non-database-column-attributes reset on reload? Something like:

class User < ActiveRecord::Base
  # this
  reloadable_attr_accessor :user_agent
  # or this
  def user_agent
    @user_agent
  end

  def user_agent=(value)
    set_instance_var_that_resets_on_reload("@user_agent", value)
  end
end

Does that exist in Rails somewhere?

  • 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-21T04:44:02+00:00Added an answer on May 21, 2026 at 4:44 am

    ActiveRecord does not provide a way to do this, it can only acts on the model attributes.

    That being said, I think a more elegant way to do it would be to loop over the ivars and set them to whatever you like :

    class User < ActiveRecord::Base
      def reload(options = nil)
        super
        self.instance_variables.each do |ivar|
          next if ivar == '@attributes'
          self.instance_variable_set(ivar, nil)      
        end
      end
     end
    

    Note that we skip @attributes because AR is taking care of it when you reload the attributes.

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

Sidebar

Related Questions

I want to create a model 'Relation' which extends ActiveRecord::Base, set it's table name
When I add has_secure_password to the model (inherited from ActiveRecord::Base), error stating that bcrypt-ruby
I have a Message model class (which inherits from ActiveRecord::Base). For a particular deployment,
I have a standard ActiveRecord model with the following: class MyModel < ActiveRecord::Base custom_method
my model is class Job < ActiveRecord::Base belongs_to :client end class Client < ActiveRecord::Base
In my model I have: class Log < ActiveRecord::Base serialize :data ... def self.recover(table_name,
I have the following classes in my ActiveRecord model: def Property < ActiveRecord::Base #
I have a Meeting model: class Meeting < ActiveRecord::Base has_one :location, :class_name => MeetingLocation,
I have this Project model: class Project < ActiveRecord::Base validates :status, :inclusion => {
I have a model in my rails application which is class Person < ActiveRecord::Base

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.