So, I thought that I had this work last night, I could swear. Now it didn’t work, and I look forward to asking for help.
Im defining dynamic fields in a database, semi EAV-style, and allows you to simply declare right now. I do not want to hear your opinions on whether EAV is a good idea or not :)
In any case, I do it a little differently than Ive done in the past, basically when an attribute (or field) is added, I create an add column to migrate the table of specific attributes and run it (or delete it) - In any case, since there is a category layer in the middle, which is a direct link where all attributes are defined, I cannot use the actual attribute name as the column name, since the attributes belong to the category.
So, if that helps you visualize
Entity belongs_to :category Category has_many :entities EntityAttribute belongs_to :category EntityAttributeValue belongs_to :entity_attribute belongs_to :entity
And the EAV table spreads horizontally as new attributes are created with columns labeled attribute_1 attribute_2, which contain values for this particular object.
In any case - I'm trying to make the methods dynamic for the entity model, so I can call @ entity.actual_attribute_name and not @ entity.entity_attribute_value.field_5
Here is the code that I thought worked -
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, when I returned to the code, I realized that I could set the attribute in the rails console, and it would return the correct field, when I saved the record, EntityAttributeValue was not updated (represented as self.project_category_field_value, above.)
Therefore, looking at it further, it seemed to me that I just needed to add a before_update or before_save callback to manually save the attribute, and exactly where I noticed in the callback, it launched the method_missing callback, as if the object (and the new object was a copy of the original object), or something else, I'm not quite sure. But at some point during saving or earlier, my attribute disappears into oblivion.
So, I assume that I halfway answered my question after entering it, I need to set the instance variable and check if it exists at the beginning of my method_missing method (right?) Maybe this is not what happens I don’t know, but I also ask if there is a better way to do what I'm trying to do.
And if using the method_missing method is a bad idea, please explain why, as I look through the messages about the missing method, I heard some people clapping it, but none of these people were worried, offering a reasonable explanation of why the method is missing - this is a bad decision.
Thanks in advance.