Dynamic Setters

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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs