PostgreSQL范围应为
fully supported
我设法将您的情况再现如下:
迁移:
#db/migrate/20171009152602_create_test_table.rb
class CreateTestTable < ActiveRecord::Migration[5.0]
def up
create_table :test_tables do |t|
t.numrange :h_range
end
end
def down
drop_table :test_tables
end
end
型号:
# app/models/test_table.rb
class TestTable < ActiveRecord::Base
end
这个
numrange
在类型支持方面存在一些挑战:
$ rails c
Loading development environment (Rails 5.0.5)
2.4.0 :001 > c = TestTable.new
=> #<TestTable id: nil, h_range: nil>
# Inserting Integer range
2.4.0 :002 > c.update(h_range:(3..5))
(0.3ms) BEGIN
SQL (0.5ms) INSERT INTO "test_tables" ("h_range") VALUES ($1) RETURNING "id" [["h_range", "[3,5]"]]
(16.7ms) COMMIT
=> true
2.4.0 :003 > c.h_range
=> 0.3e1..0.5e1
2.4.0 :004 > c.h_range.to_a
TypeError: can't iterate from BigDecimal
2.4.0 :005 > c.h_range.step
=> #<Enumerator: 0.3e1..0.5e1:step(1)>
2.4.0 :006 > c.h_range.step.to_a
=> [0.3e1, 0.4e1, 0.5e1]
# Inserting Float range
2.4.0 :007 > c.update(h_range:(3.4..5.8))
(0.3ms) BEGIN
SQL (0.8ms) UPDATE "test_tables" SET "h_range" = $1 WHERE "test_tables"."id" = $2 [["h_range", "[3.4,5.8]"], ["id", 1]]
(20.7ms) COMMIT
=> true
2.4.0 :008 > c.h_range.step.to_a
=> [0.34e1, 0.44e1, 0.54e1]
2.4.0 :009 > c.h_range.step(0.1).to_a
=> [3.4, 3.5, 3.6, 3.7, 3.8, 3.9, 4.0, 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 4.9, 5.0, 5.1, 5.2, 5.3, 5.4, 5.5, 5.6, 5.7, 5.8]
我建议你更喜欢
ActiveRecord
例子
update
方法,而不是模型中的原始sql。
.step
在您的
h_range
领域
使现代化
在您通过共享的代码之后
糊状纸盒
,在控制器中,您应该能够更新以下值:
def update
hr_params = params.require(:height_range).permit(:classification_id, :tree_id, :diameter, :range_min, :range_max)
r_min = hr_params[:range_min].to_f
r_max = hr_params[:range_max].to_f
@hr = HeighRange.find(params[:id])
hr_options = {
classification_id: hr_params[:classification_id],
tree_id: hr_params[:tree_id],
diameter: hr_params[:diameter],
h_range: (r_min..r_max)
}
@hr.update(hr_options)
if @hr.valid?
@hr.save!
redirect_to admin_height_ranges_path, notice: 'Height range was successfully updated.
else
render action: :edit, params: { id: @hr.id }
end
end