You can always use meta programming to dynamically generate all this repeated code.
module Flexible
FLEXIBLE_ATTRIBUTES.each do |attr|
define_method("flexible_#{attr}") do
self.send(attr)
end
define_method("flexible_#{attr}=") do |val|
self.send("#{attr}=", val.tr('$ ,', '')) unless val.blank?
end
end
end
Now include this module in you class and define the FLEXIBLE_ATTRIBUTES
:
class MyModel
FLEXIBLE_ATTRIBUTES = [:income, :taxes] #......
include Flexible
end
Now I haven't tested this code but it should do what you're looking for with minimum repetitiveness. It also separates the Flexible method creation logic outside of the model itself. It will also allow you to reuse this module in other classes if needed.