在我安装money-rails
gem 之前,我的控制器中有这个查询:
@forecasts.where('max >= ?', @budget).limit(1)[0]
其中:budget
是 table 中的整数列forecasts
。
安装后money-rails
,我将该列重命名为:budget_subunits
,输入monetize: :budget_subunits as: :budget
到我的:forecast
模型中,:budget
现在它是一个 Money 对象。现在此查询导致以下错误:
PG::InvalidTextRepresentation: ERROR: invalid input syntax for integer: "--- !ruby/object:Money fractional: 4000000000.0 currency: !ruby/object:Money::Currency id: :usd priority: 1 iso_code: USD name: United States Dollar symbol: $ alternate_symbols: - US$ subunit: Cent subunit_to_unit: 100 symbol_first: true html_entity: $ decimal_mark: . thousands_separator: ',' iso_numeric: '840' bank: !ruby/object:Money::Bank::VariableExchange rounding_method: rates: {} mutex: !ruby/object:Mutex {} " LINE 1: ...get_segments"."budget_source_id" = $1 AND (max >= '--- !ruby... ^ : SELECT "budget_segments".* FROM "budget_segments" WHERE "budget_segments"."budget_source_id" = $1 AND (max >= '--- !ruby/object:Money fractional: 4000000000.0 currency: !ruby/object:Money::Currency id: :usd priority: 1 iso_code: USD name: United States Dollar symbol: $ alternate_symbols: - US$ subunit: Cent subunit_to_unit: 100 symbol_first: true html_entity: $ decimal_mark: . thousands_separator: '','' iso_numeric: ''840'' bank: !ruby/object:Money::Bank::VariableExchange rounding_method: rates: {} mutex: !ruby/object:Mutex {} ') ORDER BY "budget_segments".max ASC LIMIT 1
我知道我可以只使用该:budget_subunits
列并将其与该:max_subunits
列进行比较,但这不会破坏使用 money-rails gem 的全部目的吗?
简单地说,如何在 activerecord 查询中使用 Money 对象?