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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:15:31+00:00 2026-06-01T10:15:31+00:00

So, thought I had this working last night, couldve sworn it. Now it no

  • 0

So, thought I had this working last night, couldve sworn it. Now it no worky, and I figure its about time to ask for help.

Im defining dynamic fields in the database, semi EAV style, and lets just state right now I dont care to hear your opinions on whether EAV is a good idea or not 🙂

Anyways Im doing it a little differently than Ive done in past, basically when an attribute (or field), is added, I create a add column to a particular attributes table migration and run it (or a remove it one) — ANYWAYS, because there is a category layer sitting in the middle which is the direct relationship where all the attributes are defined, I cant use the actual attribute name as a column name, as attributes are category specific.

So, if it helps you visualize

    Entity
    belongs_to :category

    Category
    has_many :entities

    EntityAttribute
    belongs_to :category

    EntityAttributeValue
    belongs_to :entity_attribute
    belongs_to :entity        

And EAV table spans horizontally as new attributes are created, with columns labeled attribute_1 attribute_2, which contain the values for that particular entity.

Anyways — I am trying to make the methods dynamic on the entity model, so I can call @entity.actual_attribute_name, rather than @entity.entity_attribute_value.field_5

Here is the code I thought was working —

    def method_missing(method, *args)

      return if self.project_category.blank?

      puts "Sorry, I don't have #{method}, let me try to find a dynamic one."
      puts "let me try to find a dynamic one"

      keys = self.project_category.dynamic_fields.collect {|o| o.name.to_sym }

      if keys.include?(method)
        field = self.project_category.dynamic_fields.select { |field| field.name.to_sym == method.to_sym && field.project_category.id == self.project_category.id }.first
        fields = self.project_category.dynamic_field_values.select {|field| field.name.to_sym == method }
        self.project_category_field_value.send("field_#{field.id}".to_sym, *args)
      end

    end

Then today as I went back to code, I realized although I could set the attribute in rails console, and it would return the correct field, when I saved the record, the EntityAttributeValue was not being updated (represented as self.project_category_field_value, above.)

So after looking into it further it looked like I just had to add a before_update or before_save callback to manually save the attribute, and thats where I noticed, in the callback, it would re run the method_missing callback, as if the object was being duplicated (and the new object was copy of original object), or something, Im not quite sure. But at somepoint during save process or before, my attribute dissapears into oblivion.

So, well I guess I half way answered my own question after typing it out, I need to set an instance variable and check to see if it exists at the beginningish of my method_missing method (right?) Maybe that’s not what’s happening I dont know, but Im also asking if there is a better way of doing what I am trying to do.

And if using method_missing is a bad idea, please explain why, as going through posts regarding method missing I heard some people slamming it but not one of those people bothered offering a reasonable explanation as to why method missing was a bad solution.

Thanks in advance.

  • 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-01T10:15:32+00:00Added an answer on June 1, 2026 at 10:15 am

    That’s some seriously intense programming to be going on in the method_missing department. What you should have is something more like this:

    def method_missing(name, *args)
      if (method_name = dynamic_attribute_method_for(name))
        method_name.send(*args)
      else
        super
      end
    end
    

    You can then try and break this down into two parts. The first is creating a method that decides if it can handle a call with a given name, here dynamic_attribute_method_for, and the second is the actual method in question. The job of the former is to ensure the latter works by the time it is called, possibly using define_method to avoid having to go through all of this again the next time you access the same method name.

    That method might look like this:

    def dynamic_attribute_method_for(name)
      dynamic_attributes = ...
    
      type = :reader
    
      attribute_name = name.to_s.sub(/=$/) do 
        type = :writer
        ''
      end
    
      unless (dynamic_attributes.include?(attribute_name))
        return
      end
    
      case (type)
      when :writer
        define_method(name) do |value|
          # Whatever you need
        end
      else
        define_method(name) do
          # Whatever you need
        end
      end
    
      name
    end
    

    I can’t tell what’s going on in your method as the structure is not clear and it seems highly dependent on the context of your application.

    From a design perspective you might find it’s easier to make a special-purpose wrapper class that encapsulates all of this functionality. Instead of calling object.attribute_name you’d call object.dynamic_attributes.attribute_name where in this case dynamic_attributes is created on demand:

    def dynamic_attributes
      @dynamic_attributes ||= DynamicAccessor.new(self)
    end
    

    When that object is initialized it will pre-configure itself with whatever methods are required and you won’t have to deal with this method missing stuff.

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

Sidebar

Related Questions

So I had this working last week. At least, I thought I did! DataGridView
I thought I had this perfectly working but apparently not. I left my project
I thought I had this figured out but it turns out I'm just deleting
Alright, I thought I had this whole setTimeout thing perfect but I seem to
I thought I had resolved this but I obviously haven't and was hoping someone
I had thought about decorating various control items with attributes to declare group ownership,
I have been working on the app engine for some time, and this problem
I thought I had this mess sorted out in my head but for some
I've been working a few days on this spinner control, though I haven't had
I thought I had this one sorted but I have run into a snag.

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.