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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:19:24+00:00 2026-06-03T07:19:24+00:00

I am using Ruby on Rails 3.2.2 and I would like to know if

  • 0

I am using Ruby on Rails 3.2.2 and I would like to know if the following is a “proper”/”correct”/”sure” way to override a setter method for a my class attribute.

attr_accessible :attribute_name

def attribute_name=(value)
  ... # Some custom operation.

  self[:attribute_name] = value
end

The above code seems to work as expected. However, I would like to know if, by using the above code, in future I will have problems or, at least, what problems “should I expect”/”could happen” with Ruby on Rails. If that isn’t the right way to override a setter method, what is the right way?


Note: If I use the code

attr_accessible :attribute_name

def attribute_name=(value)
  ... # Some custom operation.

  self.attribute_name = value
end

I get the following error:

SystemStackError (stack level too deep):
  actionpack (3.2.2) lib/action_dispatch/middleware/reloader.rb:70
  • 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-03T07:19:25+00:00Added an answer on June 3, 2026 at 7:19 am

    ===========================================================================
    Update: July 19, 2017

    Now the Rails documentation is also suggesting to use super like this:

    class Model < ActiveRecord::Base
    
      def attribute_name=(value)
        # custom actions
        ###
        super(value)
      end
    
    end
    

    ===========================================================================

    Original Answer

    If you want to override the setter methods for columns of a table while accessing through models, this is the way to do it.

    class Model < ActiveRecord::Base
      attr_accessible :attribute_name
    
      def attribute_name=(value)
        # custom actions
        ###
        write_attribute(:attribute_name, value)
        # this is same as self[:attribute_name] = value
      end
    
    end
    

    See Overriding default accessors in the Rails documentation.

    So, your first method is the correct way to override column setters in Models of Ruby on Rails. These accessors are already provided by Rails to access the columns of the table as attributes of the model. This is what we call ActiveRecord ORM mapping.

    Also keep in mind that the attr_accessible at the top of the model has nothing to do with accessors. It has a completely different functionlity (see this question)

    But in pure Ruby, if you have defined accessors for a class and want to override the setter, you have to make use of instance variable like this:

    class Person
      attr_accessor :name
    end
    
    class NewPerson < Person
      def name=(value)
        # do something
        @name = value
      end
    end
    

    This will be easier to understand once you know what attr_accessor does. The code attr_accessor :name is equivalent to these two methods (getter and setter)

    def name # getter
      @name
    end
    
    def name=(value) #  setter
      @name = value
    end
    

    Also your second method fails because it will cause an infinite loop as you are calling the same method attribute_name= inside that method.

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

Sidebar

Related Questions

I am using Ruby on Rails 3.1 and I would like to know (for
I am using Ruby on Rails 3.0.7 and I would like to know how
I am using Ruby on Rails 3.0.7 and I would like to know what
I am using Ruby on Rails 3.0.7 and I would like to know what
I am using Ruby on Rails 3.0.7 and I would like to know when
I am using Ruby on Rails 3 and I would like to know what
I am using Ruby on Rails 3.1 and I would like to know how
I am using Ruby on Rails 3 and I would like to know some
I am using Ruby on Rails 3.1.0 and I would like to know how
I am using Ruby on Rails 3 and I would like to know some

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.