Ruby on rails StoreModel gem似乎不支持阵列
我正在尝试使用StoreModel gem使用类来支持我的jsonb数据。当尝试定义数组属性时,我得到ArgumentError未知关键字:array。例如:Ruby on rails StoreModel gem似乎不支持阵列,ruby-on-rails,Ruby On Rails,我正在尝试使用StoreModel gem使用类来支持我的jsonb数据。当尝试定义数组属性时,我得到ArgumentError未知关键字:array。例如: class A include StoreModel::Model attribute :my_array, :integer, array:true, default:Array.new end 将此输入irb将产生错误 经进一步检查,它似乎与StoreModel gem没有任何关系 class A include
class A
include StoreModel::Model
attribute :my_array, :integer, array:true, default:Array.new
end
将此输入irb将产生错误
经进一步检查,它似乎与StoreModel gem没有任何关系
class A
include ActiveModel::Model
include ActiveModel::Attributes
attribute :my_array, :integer, array:true, default:Array.new
end
将产生相同的结果。根据属性API,该类不需要由数据库表支持。但是,当我尝试该示例时:
class MyModel < ActiveRecord::Base
attribute :my_string, :string
attribute :my_int_array, :integer, array: true
attribute :my_float_range, :float, range: true
end
model = MyModel.new(
my_string: "string",
my_int_array: ["1", "2", "3"],
my_float_range: "[1,3.5]",
)
在IRB中,它尝试执行SQL查询,因此我得到:
ActiveRecord::StatementInvalid (PG::UndefinedTable: ERROR: relation "my_models" does not exist)
LINE 8: WHERE a.attrelid = '"my_models"'::regclass
^
: SELECT a.attname, format_type(a.atttypid, a.atttypmod),
pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod,
c.collname, col_description(a.attrelid, a.attnum) AS comment
FROM pg_attribute a
LEFT JOIN pg_attrdef d ON a.attrelid = d.adrelid AND a.attnum = d.adnum
LEFT JOIN pg_type t ON a.atttypid = t.oid
LEFT JOIN pg_collation c ON a.attcollation = c.oid AND a.attcollation <> t.typcollation
WHERE a.attrelid = '"my_models"'::regclass
AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum
gem的作者在这里:这个问题正在讨论中–我稍后将实现真正的阵列支持,但有一个解决方法: 定义自定义类型我将它们保存在应用程序/类型中 类ArrayOfStringsType
ActiveRecord和ActiveModel都有一个属性模块。ActiveRecord::Attributes接受array:true选项(如果DB驱动程序支持)。在ActiveModel中,您使用type::array定义了一个数组属性。我怀疑gem是否真的与Rails 5兼容。我真的会考虑是否应该在这里使用JSONB列,而不是实际使用关系建模。编辑了我的原始评论