Question

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.

Was it helpful?

Solution

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

OTHER TIPS

What you are getting back from the find is an Array of Header objects.

headers = Header.find_by_sql(...)
newid = headers.first.id

I might ask myself why I need the id though

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top