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

  • Home
  • SEARCH
  • 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 913909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:37:59+00:00 2026-05-15T17:37:59+00:00

I’m trying to write a Ruby class that works similarly to Rails AactiveRecord model

  • 0

I’m trying to write a Ruby class that works similarly to Rails AactiveRecord model in the way that attributes are handled:

class Person
  attr_accessor :name, :age

  # init with Person.new(:name => 'John', :age => 30)
  def initialize(attributes={})
    attributes.each { |key, val| send("#{key}=", val) if respond_to?("#{key}=") }
    @attributes = attributes
  end

  # read attributes
  def attributes
    @attributes
  end

  # update attributes
  def attributes=(attributes)
    attributes.each do |key, val| 
      if respond_to?("#{key}=")
        send("#{key}=", val) 
        @attributes[key] = name
      end
    end
  end
end

What I mean is that when I init the class, an “attributes” hash is updated with the relevant attributes:

>>> p = Person.new(:name => 'John', :age => 30)
>>> p.attributes
 => {:age=>30, :name=>"John"}
>>> p.attributes = { :name => 'charles' }
>>> p.attributes
 => {:age=>30, :name=>"charles"}

So far so good. What I want to happen is for the attributes hash to update when I set an individual property:

>>> p.attributes
 => {:age=>30, :name=>"John"}
>>> p.name
 => "John"
>>> p.name = 'charles' # <--- update an individual property
 => "charles"
>>> p.attributes
 => {:age=>30, :name=>"John"} # <--- should be {:age=>30, :name=>"charles"}

I could do that by writing a setter and getter for every attribute instead of using attr_accessor, but that’ll suck for a model that has a lot of fields. Any quick way to accomplish this?

  • 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-15T17:38:00+00:00Added an answer on May 15, 2026 at 5:38 pm

    The problem is that you keep your attributes both as separate ivars, and within a @attributes hash. You should choose and use only one way.

    If you want to use a hash, you should make your own way of creating accessors, which would “reroute” them to a single method which would set and get from a hash:

    class Class  
     def my_attr_accessor(*accessors)
       accessors.each do |m|
    
         define_method(m) do  
           @attributes[m]
         end        
    
         define_method("#{m}=") do |val| 
           @attributes[m]=val
         end
       end
     end
    end
    
    class Foo
      my_attr_accessor :foo, :bar
    
      def initialize
        @attributes = {}
      end
    end
    
    foo = Foo.new
    
    foo.foo = 123
    foo.bar = 'qwe'
    p foo
    #=> #<Foo:0x1f855c @attributes={:foo=>123, :bar=>"qwe"}>
    

    If you want to use ivars, you should, again, roll your own attr_accessor method which would, in addition, remember which ivars should be “attributes”, and use that list in attributes method. And attributes method would create a hash out of them on-the-fly, and return it.

    Here you can find a nice article about implementing accessors.

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

Sidebar

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.