Postgres + Rails Forms Border Fields

Are there standard ways to integrate Postgres type types using Rails form helpers? I basically need the min and max field, which is converted to a range when saved. Any ideas?

+6
source share
1 answer

At first I thought of something like this:

class Model delegate :begin, :end, to: :range, prefix: true, allow_nil: true # Replace :range with your field name end 

Get methods: range_begin , range_end . I checked the documentation and these methods are read-only.

So you also need 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 do not use || in setters, you get ArgumentError: bad value for range in an empty record.

In your views, you can use regular inputs for the range_begin and range_end .

+2
source

Source: https://habr.com/ru/post/989644/


All Articles