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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:26:24+00:00 2026-06-14T18:26:24+00:00

Typically when writing a model in Rails you use a DSL to setup various

  • 0

Typically when writing a model in Rails you use a DSL to setup various aspects of derived objects, for example:

class Question < ActiveRecord::Base
  has_one :category
  validates_presence_of :category
end

In this case, “has_one” and “validates_presence_of” create associations and validation call backs on models instantiated from Question.

I want to add a new method called “parent” to be used when defining a class:

class Question
  attr_accessor :category

  parent :category
end

q = Question.new
q.category = 'a category'
puts q.parent
-> 'a category'

So when objects are instantiated from class, they should have the method “parent” defined.

How do I do this? My first thought was to use a module, but this isn’t an instance method, or a class method.

  • 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-14T18:26:26+00:00Added an answer on June 14, 2026 at 6:26 pm

    I believe this is what you are looking for:

    module QuestionParent
      module ClassMethods
        def inherited(descendant)
          descendant.instance_variable_set(:@parent, parent.dup)
          super
        end
    
        def parent(args=nil)
          @parent ||= args
        end
      end
    
      module InstanceMethods
        def parent
          self.send self.class.parent.to_sym
        end
      end
    
      def self.included(receiver)
        receiver.extend         ClassMethods
        receiver.send :include, InstanceMethods
      end
    end
    
    class Question
      include QuestionParent
    
      attr_accessor :category
    
      parent :category
    end
    

    Which produces:

    q = Question.new
    q.category = 'a category'
    puts q.parent
    
    a category
    

    What this does is add a class method parent that will define the class variable @parent, when an instance calls the parent in the InstanceMethod the @parent symbol (here is category) is called.

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

Sidebar

Related Questions

When writing my programs I typically use the java.util.Properties class to load a properties
Typically, I've seen people use the class literal like this: Class<Foo> cls = Foo.class;
I'm writing a class interface that needs to return references to binary files. Typically
I am writing a client for a server that typically sends data as strings
I'm writing a program, foo, in C++. It's typically invoked on the command line
Typically you start a service like this Intent i = new Intent(context,MessageService.class); context.startService(i); but
Typically if we just use alert(object); it will show as [object Object] . How
I am a fan of Martin Fowler's (deprecated) model-view-presenter pattern. I am writing a
I am writing various ASP.NET Server controls and am needing to remove the tags
I am writing a class library(API) in C#. The class is non-static and contains

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.