I need to perform these SQL below, but I couldn't get the result.
newid=Header.find_by_sql(
"SELECT coalesce(max(transaction_id),0)+1 AS id
FROM transaction_headers
WHERE transaction_year = #{Time.now.year} AND
transaction_type='#{fields[:transaction_type]}'"
)
But I can't seem to get the result to newid. The only value I got was [#<Header>] Anyone can help me to create the correct statement for Ruby?
EDIT: Table fields
----------------------------------------------------------------------------------- | transaction_type | transaction_year | transaction_id | customer_id | uid | date | -----------------------------------------------------------------------------------
Thank you.
You are getting a Header object (which will be populated with an id), so I think in your case newid.id will actually give you the id you're looking for, or if you want an array of these values do:
headers = Header.find_by_sql(...)
header_ids = headers.collect(&:id)
HTH