I have a model with a handful of related date fields.
def started_at_date=(value)
@started_at_date = value
end
def completed_at_date=(value)
@completed_at_date = value
end
...
The getters are handled via method_missing and that works great.
def method_missing(method, *args, &block)
if method =~ /^local_(.+)$/
local_time_for_event($1)
elsif method =~ /^((.+)_at)_date$/
self.send :date, $1
elsif method =~ /^((.+)_at)_time$/
self.send :time, $1
else
super
end
end
def date(type)
return self.instance_variable_get("@#{type.to_s}_date") if self.instance_variable_get("@#{type.to_s}_date")
if self.send type.to_sym
self.send(type.to_sym).in_time_zone(eventable.time_zone).to_date.to_s
end
end
...
I’d like to add the setters dynamically, but I’m not sure how to do so in a way that avoids ActiveRecord::UnknownAttributeErrors.
I think this would work: