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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:10:10+00:00 2026-06-04T03:10:10+00:00

I have an object that inherits from ActiveRecord, yet it has an attribute that

  • 0

I have an object that inherits from ActiveRecord, yet it has an attribute that is not persisted in the DB, like:

 class Foo < ActiveRecord::Base
   attr_accessor :bar
 end

I would like to be able to track changes to ‘bar’, with methods like ‘bar_changed?’, as provided by ActiveModel Dirty. The problem is that when I try to implement Dirty on this object, as described in the docs, I’m getting an error as both ActiveRecord and ActiveModel have defined define_attribute_methods, but with different number of parameters, so I’m getting an error when trying to invoke define_attribute_methods [:bar].

I have tried aliasing define_attribute_methods before including ActiveModel::Dirty, but with no luck: I get a not defined method error.

Any ideas on how to deal with this? Of course I could write the required methods manually, but I was wondering if it was possible to do using Rails modules, by extending ActiveModel functionality to attributes not handled by ActiveRecord.

  • 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-06-04T03:10:11+00:00Added an answer on June 4, 2026 at 3:10 am

    I’m using the attribute_will_change! method and things seem to be working fine.

    It’s a private method defined in active_model/dirty.rb, but ActiveRecord mixes it in all models.

    This is what I ended up implementing in my model class:

    def bar
      @bar ||= init_bar
    end
    def bar=(value)
      attribute_will_change!('bar') if bar != value
      @bar = value
    end
    def bar_changed?
      changed.include?('bar')
    end
    

    The init_bar method is just used to initialise the attribute. You may or may not need it.

    I didn’t need to specify any other method (such as define_attribute_methods) or include any modules.
    You do have to reimplement some of the methods yourself, but at least the behaviour will be mostly consistent with ActiveModel.

    I admit I haven’t tested it thoroughly yet, but so far I’ve encountered no issues.

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

Sidebar

Related Questions

Example: I have an class that inherits from UIImageView. An object creates an instance
In Javascript I have a child object that inherits from a base object. I
I have a class that inherits from TreeNode, called ExtendedTreeNode. To add an object
I have a base class Base, and class A/B that inherits from it. public
I have a class that inherits from a base class. This base class exposes
I have a base class called Object. PhysicsObject inherits from Object. Ball inherits from
I have a custom object that simply inherits from NSObject. It has 3 members
I have a custom Grid view object that inherits from the base System.Web.UI.WebControls.GridView. The
I have an object called Settings that inherits from NSMutableDictionary . When I try
I have a toolbar object that inherits from UIView and is a subview of

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.