Reputation: 51
I am trying to fetch the result of a database function to store it in a variable. In the case of this function, a receipt id is passed and the function returns TRUE
or FALSE
depending on whether the receipt is enabled. Try in the following ways:
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"]]
I would need to do something similar to this
if result == true
@showreceipt = true
else
@showreceipt = false
end
Upvotes: 0
Views: 88
Reputation: 15288
ActiveRecord::Result
has cast_values
method (it uses deserialize
under the hood)
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
Upvotes: 1