我正在考虑使用新的 Rails 5 attributes API对于自定义数据类型,理想情况下将数据存储在两个数据库列中,一个用于数据 value
,一个用于一些额外的 type
信息。
Attributes API 似乎设计为仅使用一个数据库列,我想知道我是否缺少使用两个列的方法。
例子
设想一个 Money 对象,其中一个 decimal
或 integer
列用于值,一个 string
列用于货币代码。我会传入我的自定义货币对象,将其存储两列,然后读回它会将两列组合成一个货币对象。
我考虑过将值和货币序列化到单个 Postgres JSON 列中,但我希望能够执行快速 SQL SUM
查询并仅对值列进行排序,所以这不会看起来不太理想。
提前感谢您的任何见解。
最佳答案
我猜你正在考虑创建一个 ValueObject在您的模型中。
有ActiveRecord::Aggregations为了那个原因。示例:
class Customer < ActiveRecord::Base
composed_of :balance, class_name: "Money", mapping: %w(balance amount)
end
class Money
include Comparable
attr_reader :amount, :currency
EXCHANGE_RATES = { "USD_TO_DKK" => 6 }
def initialize(amount, currency = "USD")
@amount, @currency = amount, currency
end
def exchange_to(other_currency)
exchanged_amount = (amount * EXCHANGE_RATES["#{currency}_TO_#{other_currency}"]).floor
Money.new(exchanged_amount, other_currency)
end
def ==(other_money)
amount == other_money.amount && currency == other_money.currency
end
def <=>(other_money)
if currency == other_money.currency
amount <=> other_money.amount
else
amount <=> other_money.exchange_to(currency).amount
end
end
end
关于ruby-on-rails - 有没有办法用两个数据库列来支持新的 Rails 5 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37551832/