我正在尝试获取数据库函数的结果并将其存储在变量中。在此函数中,会传递一个收据 ID,并且根据是否启用收据,函数会返回
TRUE
或 FALSE
。请尝试以下方法:
result = ActiveRecord::Base.connection.execute("SELECT receipt_enabled FROM schema.receipt_enabled(158800)")
=> #<PG::Result:0x00556c7c374158 @connection=#<PG::Connection:0x00556c7d8ab3f0 @socket_io=nil, @notice_receiver=nil, @notice_processor=nil>>
result = ActiveRecord::Base.connection.execute("SELECT receipt_enabled FROM schema.receipt_enabled(158800)").to_a
=> [{"receipt_enabled"=>"f"}]
result = ActiveRecord::Base.connection.select_all("SELECT receipt_enabled FROM schema.receipt_enabled(158800)").rows
=> [["f"]]
我需要做类似的事情
if result == true
@showreceipt = true
else
@showreceipt = false
end
ActiveRecord::Result
有 cast_values
方法(它在底层使用 deserialize
)
query =
<<~SQL
SELECT receipt_enabled
FROM schema.receipt_enabled(158800)
SQL
ActiveRecord::Base.connection.exec_query(query).cast_values.flatten.first
# will return true or false