# Dynamic Setters

**URL:** <https://www.ruby-forum.com/t/dynamic-setters/185208>\
**Category:** Rails\
**Created:** [March 16, 2010, 5:24am UTC](https://www.ruby-forum.com/t/dynamic-setters/185208 "2010-03-16T05:24:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![PsiPro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ba8739/32.png) [@PsiPro](https://www.ruby-forum.com/u/PsiPro)\
**Post date:** [March 16, 2010, 5:24am UTC](https://www.ruby-forum.com/t/dynamic-setters/185208/1 "2010-03-16T05:24:26Z")

</div>

Hello all,

I’m tinkering around with method\_missing, and have run into a problem.

I’m trying to create virtual columns so plugins like formtastic work  
properly with this model.

I plan on wrapping all attributes related to this project with the  
name “option\_\*”, as such I have changed method missing to the  
following:

…  
def method\_missing(method, \*arguments, &block)  
logger.info “Looking for #{method}”  
if method.to\_s =~ /option\_(.+)/  
process\_option\_method(method, \*arguments)  
else  
super  
end  
end  
def process\_option\_method(method, \*arguments)  
if method.to\_s =~ /option\_(.+)=/  
return set\_option($1, \*arguments.first)  
elsif method.to\_s =~ /option\_(.+)/  
return get\_option($1, false)  
end  
end  
…

now get\_option and set\_option work as expected, and the setter part of  
the above code also works. However when a form is submitted with a  
field option\_foo i get the error “unknown attribute: option\_foo”.

The logger input above indicates that method\_missing is not called,  
and the stack trace confirms this. The stack trace indicates that when  
update\_attributes is called, it never gets around to method missing.

So, I’m stuck. What would be the proper way to accomplish a dynamic  
setter method?

Brian

---

<div class="post-metadata">

**Author:** ![PsiPro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ba8739/32.png) [@PsiPro](https://www.ruby-forum.com/u/PsiPro)\
**Post date:** [March 16, 2010, 5:43am UTC](https://www.ruby-forum.com/t/dynamic-setters/185208/2 "2010-03-16T05:43:09Z")

</div>

Solved my own problem…

for anyone else with the same issue, update\_attributes cheks to see if  
the attribute exists by querying responds\_to?(attr\_name)

It was necessary to overload that method to get it working. On to the  
next problem.

Brian
