在 Padrino Active Record 中将列类型转换为 bigint
Type Casting a Column to bigint in Padrino Active Record
我已经在 Padrino 应用程序上升级到最新的 Ruby,但在创建新对象时我遇到了活动记录错误。它将一个数字(ISBN)验证为 4 个字节。错误是:
9781407005416 is out of range for ActiveRecord::Type::Integer with limit 4
如何告诉 Ruby 活动记录 class 使用 8 字节限制?
我不是在谈论 运行 迁移,table 已经是 bigint(20)
。
我认为以下方法可行:
class Book < ActiveRecord::Base
attribute :isbn, :integer, :limit => 8
end
然后我在 ISBN 字段的 find_by
和 as_json
上收到错误:
#<NoMethodError: undefined method 'type_cast_for_database' for :integer:Symbol
我把'isbn'方法变成了一个新的实例变量,而不是改变活动记录适配器创建的属性的列。
我知道 column has a limit,但不确定如何访问列的类型来更新它。
类型设置的解决方案是否有一种方法可以跳过活动记录中正在发生的验证ensure_in_range
。
有没有办法从我的应用中更改 class Integer < Value
的内置 DEFAULT_LIMIT = 4
?这与 following changes in Ruby 有关,但我无权更改 Padrino 源。
尝试:
validates_inclusion_of :isbn, in: 1..4294967296
(即2^32,即最大4字节整数。)
或者对于 8 个字节,尝试:
validates_inclusion_of :isbn, in: 1..1.8446744e+19
确认这个非常大的范围是有效的:
> 1e10.in?(1..1.8446744e+19)
true
> 1e20.in?(1..1.8446744e+19)
false
(我假设 Padrino 的 AR 工作方式与 Rails 相同。)
How do I tell a Ruby active record class to use 8 byte limit?
AFAIK, AR should figure this out directly from the database metadata.
首先,如果尚未完成,请重新启动应用程序 - 列类型已缓存。接下来,我将验证 AR 是否确实按照您的预期看到了该列。试试这个以确保它按照您的想法行事:
p Book.columns.select{ |col| col.name == 'isbn'}
查看@cast_type下的@range和@limit。
I turned the 'isbn' method into a new instance variable instead of
changing the column of the attribute created by the active record
adapter.
这是否意味着您覆盖了 isbn getter?如果是这样,该代码到底是什么样的?
我已经在 Padrino 应用程序上升级到最新的 Ruby,但在创建新对象时我遇到了活动记录错误。它将一个数字(ISBN)验证为 4 个字节。错误是:
9781407005416 is out of range for ActiveRecord::Type::Integer with limit 4
如何告诉 Ruby 活动记录 class 使用 8 字节限制?
我不是在谈论 运行 迁移,table 已经是 bigint(20)
。
我认为以下方法可行:
class Book < ActiveRecord::Base
attribute :isbn, :integer, :limit => 8
end
然后我在 ISBN 字段的 find_by
和 as_json
上收到错误:
#<NoMethodError: undefined method 'type_cast_for_database' for :integer:Symbol
我把'isbn'方法变成了一个新的实例变量,而不是改变活动记录适配器创建的属性的列。
我知道 column has a limit,但不确定如何访问列的类型来更新它。
类型设置的解决方案是否有一种方法可以跳过活动记录中正在发生的验证ensure_in_range
。
有没有办法从我的应用中更改 class Integer < Value
的内置 DEFAULT_LIMIT = 4
?这与 following changes in Ruby 有关,但我无权更改 Padrino 源。
尝试:
validates_inclusion_of :isbn, in: 1..4294967296
(即2^32,即最大4字节整数。)
或者对于 8 个字节,尝试:
validates_inclusion_of :isbn, in: 1..1.8446744e+19
确认这个非常大的范围是有效的:
> 1e10.in?(1..1.8446744e+19)
true
> 1e20.in?(1..1.8446744e+19)
false
(我假设 Padrino 的 AR 工作方式与 Rails 相同。)
How do I tell a Ruby active record class to use 8 byte limit? AFAIK, AR should figure this out directly from the database metadata.
首先,如果尚未完成,请重新启动应用程序 - 列类型已缓存。接下来,我将验证 AR 是否确实按照您的预期看到了该列。试试这个以确保它按照您的想法行事:
p Book.columns.select{ |col| col.name == 'isbn'}
查看@cast_type下的@range和@limit。
I turned the 'isbn' method into a new instance variable instead of changing the column of the attribute created by the active record adapter.
这是否意味着您覆盖了 isbn getter?如果是这样,该代码到底是什么样的?