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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:30:05+00:00 2026-05-30T22:30:05+00:00

ActiveRecord offers attribute helper methods such as _? and the dirty methods ( _changed?

  • 0

ActiveRecord offers attribute helper methods such as _? and the “dirty” methods (_changed? etc.)

Is there a Rails way to define these same methods on non-persisted or “virtual” attributes?

I’m hoping for something like:

class MyClass < ActiveRecord::Base

  some_macro :my_attribute

end


$ @my_class = MyClass.new
$ @my_class.my_attribute? # => false
$ @my_class.my_attribute_changed? # => false
  • 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-30T22:30:06+00:00Added an answer on May 30, 2026 at 10:30 pm

    Well this was certainly something interesting to investigate. Apparently there is not a straight forward way to do this… here are two things I found

    From 2009

    From 2011 – reinforces the 2009 post but makes it a bit cleaner. You create a module that updates the attribute hash. From the Brandon Weiss post:

    # app/models/dirty_associations.rb
    module DirtyAssociations
      attr_accessor :dirty
    
      def make_dirty(record)
        self.dirty = true
      end
    
      def changed?
        dirty || super
      end
    end
    
    # app/models/lolrus.rb
    class Lolrus
      include DirtyAssociations
    
      has_and_belongs_to_many :buckets,
                              :after_add    => :make_dirty,
                              :after_remove => :make_dirty
    end
    

    There is also this plugin mentioned here but I am not sure how useful it is for you.

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

Sidebar

Related Questions

(using Ruby on Rails and ActiveRecord) In short: Is there some way how a
Given an ActiveRecord method or named_scope chain, is there a way to return the
i've got these classes: class Game < ActiveRecord::Base has_many :offers #table has an integer-column
Please help with ActiveRecord testing. Trying my first Rails 3.1.0 project. There I have
ActiveRecord's has_many and belongs_to methods both take a :foreign_key option. If I need to
Experienced with Rails / ActiveRecord 2.1.1 You create a first version with (for example)
Suppose you have an ActiveRecord::Observer in one of your Ruby on Rails applications -
Rails and ActiveRecord do a perfectly nice job of dealing with what I would
I'm trying to define a named_scope for all my models in a Rails application.
I want to create a Rails (2.1 and 2.2) model with ActiveRecord validations, but

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.