Are there any standard ways of integrating the Postgres range types with the Rails form helpers? I basically need a min and max field that get converted into a range on saving. Any ideas?
At first I was thinking about something like this:
class Model
delegate :begin, :end, to: :range, prefix: true, allow_nil: true # Replace :range with your field name
end
To get methods: range_begin
, range_end
. I checked documentation and these methods are read only.
So you need also setters:
class Model
delegate :begin, :end, to: :range, prefix: true, allow_nil: true
def range_begin=(value)
self.range = Range.new(value, (range_end || value))
end
def range_end=(value)
self.range = Range.new((range_begin || value), value)
end
end
If you don't use ||
in setters, then you get ArgumentError: bad value for range
on empty record.
In your views you can use normal inputs for fields range_begin
and range_end
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With