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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:54:04+00:00 2026-05-11T16:54:04+00:00

I have a rather unique class that allows its child classes to declare virtual

  • 0

I have a rather unique class that allows its child classes to declare virtual fields. The child can declare virtual fields stored as XML by calling a method of the parent class like this:

class Child1 < Parent
  create_xml_field ["readings", "usage"]
end

I have managed to get it working via a nasty work around. The create_xml_field method stores the field names in Class variable (see below). The init_xml_fields method is called from inside the after_initialize method.

class Parent < ActiveRecord::Base

  def self.create_xml_field(fields)
    @@xml_fields[self.name] = fields
  end

  def init_xml_fields(xml_fields)
    xml_fields.each do |f|
      f=f.to_sym
      self.class_eval do
        define_method(f) { ... } # define getter
        define_method(f) { ... } # define setter
        attr_accessible(f)       # add to mass assign OK list, does not seem to work!
      end
    end
  end

  protected
    def after_initialize
      init_xml_fields
    end
end

Nasty enough eh? I’m not proud, but I am having trouble making it work. Also, the work around doesn’t work with mass-assignment of form parameters.

Does anyone have experience calling attr_acessible dynamically to allow mass-assignment in the child class? Thank you in advance!

This post was edited for clarity!

  • 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-11T16:54:04+00:00Added an answer on May 11, 2026 at 4:54 pm

    Here’s how I’d implement the metaprogramming part that creates the accessor methods and sets them as attr_accessibles.

    I’m using YAML intead of XML just as a personal crusade. I even went ahead and implemented the unneeded serialization part just to nudge you towards YAML.

    require 'yaml'
    require 'rubygems'
    require 'active_support'
    require 'active_record'
    
    module Yamlable
      def self.included m
        m.extend ClassMethods
      end
    
      module ClassMethods
        def add_yaml_fields *args
          write_inheritable_array(:yaml_fields, args)
          attr_accessor(*args)
          attr_accessible(*args)
          before_save :serialize_yaml_fields
        end
      end
    
      def serialize_yaml_fields
        self.yamlable_column = read_inheritable_attribute(:yaml_fields)\
          .inject({}) { |h, a| h[a] = send(a); h }.to_yaml
      end
    
      def initialize(*args)
        super
        YAML::load(yamlable_column).each { |k, v| send("#{k}=", v) }
      end
    end
    
    class ParentModel < ActiveRecord::Base
      include Yamlable
      add_yaml_fields :foo, :bar
    end
    
    class ChildModel < ParentModel
    end
    
    # look, they're there:
    y ChildModel.read_inheritable_attribute(:yaml_fields)
    

    Now, if you want to know why your particular code didn’t work, you’ll have to post more of it.


    I should probably expand a bit on class inheritable attributes. They are a bit like class variables, a bit like class instance variables.

    If you define an inheritable attribute in a class, all its subclasses will share it. But if you update said attribute in a child class, this child class copies the original attribute and updates it, so the updates are exclusive to it and don’t affect other classes around it in the inheritance chain.

    With the normal write_inheritable_attribute method, setting it on a child class will simply override the value from the parent. With inheritable arrays and hashes, write_inheritable_* will concat / merge to the parent class’s values.


    So, in practice, my add_yaml_fields works like this:

    class Parent
      add_yaml_attributes :foo
    
    class Child1 < Parent
      add_yaml_attributes :bar
    
    class Child2 < Parent
      add_yaml_attributes :baz
    

    With that, the yaml attributes for each class will be:

    • Parent: foo
    • Child1: foo, bar
    • Child2: foo, baz
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 136k
  • Answers 136k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer OK, I can answer my own question here. It seems… May 12, 2026 at 7:07 am
  • Editorial Team
    Editorial Team added an answer An option would be to do: bool get_testInt16(int16% testInt16) {… May 12, 2026 at 7:07 am
  • Editorial Team
    Editorial Team added an answer SQL Server Full Text Search (FTS): Highly optimized Supports multiple… May 12, 2026 at 7:07 am

Related Questions

I'm trying to figure out the best way to save (serialize) and later open
I have a Django project whereby every model inherits from a common Object model
My situation is that I screwed up essentially. I inherited my code base about
I have a class Employee. I want to be able to Validate() it before

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.